Concept Flow - Conditional compilation
Start
Check #ifdef or #ifndef
Compile block
Continue compilation
End
The compiler checks if a condition is true (#ifdef/#ifndef). If yes, it compiles the code block; if no, it skips it and continues.
#define DEBUG #ifdef DEBUG printf("Debug mode on\n"); #endif printf("Program running\n");
| Step | Directive | Condition | Result | Action | Output |
|---|---|---|---|---|---|
| 1 | #define DEBUG | N/A | DEBUG defined | Define DEBUG macro | |
| 2 | #ifdef DEBUG | Is DEBUG defined? | Yes | Compile next block | |
| 3 | printf("Debug mode on\n"); | N/A | N/A | Execute print | Debug mode on |
| 4 | #endif | N/A | End if | End conditional block | |
| 5 | printf("Program running\n"); | N/A | N/A | Execute print | Program running |
| 6 | End | N/A | N/A | Program ends |
| Macro | Start | After Step 1 | Final |
|---|---|---|---|
| DEBUG | undefined | defined | defined |
#ifdef MACRO // code compiles only if MACRO is defined #endif #ifndef MACRO // code compiles only if MACRO is NOT defined #endif Use #define MACRO to define macros. Conditional compilation controls which code is included before compiling.