Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
#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
bgColor#ccccff
#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.

...