...
Code Block | ||
---|---|---|
| ||
#define sum 2+3a+b /* ... */ int result = sum*4; |
The value of result
is 14 a+(b*4) instead of the expected 20(a+b)*4.
Compliant Solution
Parenthesizing the macro yields the expected answer.
Code Block | ||
---|---|---|
| ||
#define sum (2a+3)b) /* ... */ int result = sum*4; /* 20 */ |
Risk Assessment
Failing to parenthesize around a function-like macro can result in unexpected arithmetic results.
...