Macro Usage
Parenthesize Macro Definitions
When defining macros that expand to a value or expression, always wrap the entire definition in parentheses. Without parentheses, the macro's expansion can interact unexpectedly with surrounding operators due to C's operator precedence rules.
Wrong:
#define NEG -1
Correct:
#define NEG (-1)
Without the parentheses, a use like x * NEG expands to x * -1, which is fine here — but more complex expressions can produce surprising results. For example, a macro like:
#define DOUBLE(x) x + x
used in 10 * DOUBLE(5) expands to 10 * 5 + 5, which evaluates as 55, not 100. The corrected definition:
#define DOUBLE(x) ((x) + (x))
expands to 10 * ((5) + (5)), giving the expected 100.
The same rule applies to macro parameters: each parameter use inside the macro body should also be wrapped in parentheses to guard against cases where the argument itself is an expression with lower-precedence operators.