Macro functions in C defined via #define perform raw textual replacement at compile-time before code is compiled. Without enclosing arguments in parentheses, arithmetic operator precedence produces unexpected calculation errors.
Parameterized Macro Parentheses Example
#include <stdio.h>
// WRONG: #define SQUARE(x) x * x -> SQUARE(2 + 3) expands to 2 + 3 * 2 + 3 = 11!
// CORRECT: Parenthesize arguments and whole expression
#define SQUARE(x) ((x) * (x))
int main(void) {
int val = 2 + 3;
printf("Square of (2 + 3) = %d\n", SQUARE(val)); // Correctly evaluates to 25
return 0;
}
Comments and corrections