Loading lessons...
The Preprocessor
The Preprocessor
Before compiling, a preprocessor scans the file. It handles # directives.
Includes
C
#include <stdio.h> // standard library
#include "myfile.h" // your own headerMacros (defines)
C
#define PI 3.14159Everywhere PI appears, the text 3.14159 replaces it before compile.
Macro with an argument
C
#define MAX(a, b) ((a) > (b) ? (a) : (b))Why macros?
Useful for constants and quick helpers, but macros are error-prone. Prefer const when possible.
TL;DR
- Runs before compilation.
- Handles #include and #define.
- Macros are simple text swaps.
- Prefer constants over overkill macros.