← Preprocessor and CompilationC PROGRAMMING 1.6.A
Preprocessor Directives
#define, #include, and the rest of the directive set.
The directive set is all pure text substitution — none of it is real C, it all happens before the compiler proper ever runs, which is the same pipeline stage covered back in 1.1.A.
#define MAX_SIZE 128
#define SQUARE(x) ((x) * (x)) /* parens around x and the whole expression matter */
int buf[MAX_SIZE];
int y = SQUARE(3 + 1); /* expands to ((3 + 1) * (3 + 1)) = 16, not 3 + 1 * 3 + 1 = 7 */Function-like macros are textual, not real function calls — no type checking, no scoping, just direct substitution. SQUARE(x) without the parens around x would expand SQUARE(3+1) into 3+1 * 3+1, which is a completely different (wrong) expression thanks to operator precedence. Parenthesize every macro parameter, and the whole expansion, out of habit.
Notes from readers
Comments — via GitHub