Lesson 32 / 36
Macros & Conditional Compilation
Text substitution before compilation even starts.
#define is a text swap
The preprocessor runs before the compiler and blindly replaces macro names with their text — no type checking happens at this stage.
Object-like and function-like macros
Wrap macro parameters in parentheses to avoid operator-precedence bugs.
#define MAX_SIZE 100
#define SQUARE(x) ((x) * (x))
int arr[MAX_SIZE];
printf("%d\n", SQUARE(3 + 2)); // 25, not 11
Output:
25
#ifdef for conditional code
Code inside #ifdef blocks is only included by the preprocessor if the macro is defined — common for debug logging or platform-specific code.
#define DEBUG
#ifdef DEBUG
printf("debug: x = %d\n", x);
#endif