What Is the Preprocessor?
The C preprocessor runs before compilation. It performs textual transformations on your source code — including files, expanding macros, and conditionally compiling sections. All preprocessor directives start with
#.#include — File Inclusion
c
The preprocessor literally pastes the entire contents of the included file into your source.
#define — Constants and Macros
c
Warning: Always parenthesize macro parameters and the whole expression to prevent operator precedence bugs.
Conditional Compilation
c
Include Guards
Prevent a header from being included multiple times:
c
Or use the non-standard but widely supported:
c
Predefined Macros
| Macro | Value |
|---|---|
__FILE__ | Current filename |
__LINE__ | Current line number |
__DATE__ | Compilation date |
__TIME__ | Compilation time |
__func__ | Current function name (C99) |
c
#pragma — Compiler Directives
c
Summary
| Directive | Purpose |
|---|---|
#include | Paste file contents |
#define | Create macros/constants |
#ifdef / #ifndef | Conditional compilation |
#pragma | Compiler-specific hints |
#undef | Remove a macro definition |
#error | Force a compile error |