The C preprocessor runs before the compiler. It processes lines that start with # (directives), expands macros, includes header files, and keeps or removes blocks of code with conditional compilation. This section covers #include, #define, object-like and function-like macros, #if / #ifdef, header guards, predefined macros, and #error / #pragma.
Macros are text, not code.
The preprocessor knows nothing about types or scope. Parenthesize macro parameters and the whole expansion to avoid surprising precedence.
Path
Learning Path
Work through the stages in order, or jump to a topic from the index below (9 tutorials).
#define SQ(x) x*x makes SQ(a+1) become a+1*a+1. Wrap every parameter and the whole expansion.
side
Repeated evaluation
SQUARE(i++) evaluates i++ twice. Use an inline function instead.
semi
Trailing semicolons
#define N 10; leaks the semicolon into every use.
Remember
Key Takeaways
Text: macros are substituted before compilation.
Guard: protect headers.
Prefer:const, enum, and inline when they fit.
One line: The C preprocessor runs before the compiler.
Frequently Asked Questions
It is a text-processing step before compilation that handles #include, #define, conditional compilation, and other directives.
A macro is replaced by text before compilation and has no type checking or call overhead. A function is compiled code that is called at run time and evaluates arguments once.
Compile with gcc -E file.c to print the code after preprocessing.