Preprocessor and Build Boundaries
Macro Constants
An object-like macro can name a compile-time text replacement, while a normal variable still changes at runtime.
Macro Constants
macro_constants.cpp
#include <iostream>
#define TAX_PERCENT 8
#if TAX_PERCENT == 8
constexpr int taxPercent = 8;
#else
constexpr int taxPercent = 0;
#endif
int main() {
int subtotal = ;
int tax = subtotal * taxPercent / 100;
int total = subtotal + tax;
std::cout << "subtotal=" << subtotal << std::endl;
std::cout << "tax=" << tax << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#define TAX_PERCENT 8
#if TAX_PERCENT == 8
constexpr int taxPercent = 8;
#else
constexpr int taxPercent = 0;
#endif
int main() {
int subtotal = ;
int tax = subtotal * taxPercent / 100;
int total = subtotal + tax;
std::cout << "subtotal=" << subtotal << std::endl;
std::cout << "tax=" << tax << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#define TAX_PERCENT 8
#if TAX_PERCENT == 8
constexpr int taxPercent = 8;
#else
constexpr int taxPercent = 0;
#endif
int main() {
int subtotal = ;
int tax = subtotal * taxPercent / 100;
int total = subtotal + tax;
std::cout << "subtotal=" << subtotal << std::endl;
std::cout << "tax=" << tax << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
macro constant
A macro constant is replaced by the preprocessor before the compiler sees the program.