Preprocessor Directives & Macros
Code that runs BEFORE compilation even starts.
#include
Tells the preprocessor to insert the content of another file (usually a header file) before compilation. <stdio.h> in angle brackets looks in system directories; "myheader.h" in quotes looks in your own project folder first.
#define — Macros
#define creates a macro — a piece of text that gets textually replaced everywhere it appears, before compilation. It's often used for constants or simple reusable expressions.
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
printf("%f", PI);
printf("%d", SQUARE(5)); // becomes ((5)*(5)) = 25Conditional Compilation
#ifdef, #ifndef, and #endif let you include or exclude code based on conditions, often used to prevent a header file from being included twice (header guards), or to include debug-only code.
🌍 Real-World Use
Nearly every C program uses #include to bring in standard library functions. Large codebases use header guards (#ifndef/#define/#endif) in every header file to prevent errors from accidentally including the same file twice.
💡 Pro Tip
Always wrap macro parameters in parentheses, like ((x) * (x)) instead of x * x — without the parentheses, SQUARE(2+3) would expand to 2+3 * 2+3 = 11 instead of the expected 25, a classic and confusing macro bug.
🧪 Quick Self-Test
Check what you just learned — no pressure, just practice.
1. When does the preprocessor run relative to compilation?
2. Why should macro parameters be wrapped in parentheses?