June 2025
Intermediate to advanced
1093 pages
33h 24m
English
It is particularly important that the preprocessor does not introduce or remove parentheses but only performs a textual replacement, especially when macro parameters are used in the replacement expression.
#define SQUARE(x) x*xint result = SQUARE(3+4)
is evaluated to 19, as you can see here. However, SQUARE(3+4) should probably be 7*7, which is 49. This is very dangerous:
int result = 3+4*3+4
Normally, you should always enclose each use of a parameter in the replacement text with brackets. And often, the entire replacement text also needs to be enclosed in brackets.
#define SQUARE(x) ((x)*(x))int result = SQUARE(3+4)
becomes what you want, because the following calculation results in 49:
int result
Read now
Unlock full access