0
0
Cprogramming~10 mins

Conditional compilation - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
C
#define DEBUG
#ifdef DEBUG
printf("Debug mode on\n");
#endif
printf("Program running\n");
This code prints a debug message only if DEBUG is defined, then always prints 'Program running'.
Execution Table
StepDirectiveConditionResultActionOutput
1#define DEBUGN/ADEBUG definedDefine DEBUG macro
2#ifdef DEBUGIs DEBUG defined?YesCompile next block
3printf("Debug mode on\n");N/AN/AExecute printDebug mode on
4#endifN/AEnd ifEnd conditional block
5printf("Program running\n");N/AN/AExecute printProgram running
6EndN/AN/AProgram ends
💡 Reached end of code, all directives processed.
Variable Tracker
MacroStartAfter Step 1Final
DEBUGundefineddefineddefined
Key Moments - 2 Insights
Why does the code inside #ifdef DEBUG run only sometimes?
Because the code inside #ifdef DEBUG runs only if the macro DEBUG is defined (see execution_table step 2). If DEBUG is not defined, that block is skipped.
What happens if we remove #define DEBUG at the start?
The condition at step 2 becomes false, so the debug print is skipped and only 'Program running' prints (see execution_table step 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 3?
ANo output
BProgram running
CDebug mode on
DCompilation error
💡 Hint
Check the Output column at step 3 in the execution_table.
At which step does the compiler decide to skip or compile the debug print?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the Condition and Result columns in execution_table step 2.
If #define DEBUG is removed, what changes in the execution?
AThe debug print is skipped
BThe program will not compile
CBoth prints run
DOnly debug print runs
💡 Hint
Refer to key_moments explanation and execution_table step 2.
Concept Snapshot
#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.
Full Transcript
Conditional compilation in C uses directives like #ifdef and #ifndef to include or exclude code blocks based on whether a macro is defined. The compiler checks these conditions before compiling. For example, if DEBUG is defined with #define DEBUG, then code inside #ifdef DEBUG runs; otherwise, it is skipped. This lets you include debug messages or platform-specific code without changing the main program. The execution table shows each step: defining macros, checking conditions, compiling or skipping code, and printing output. Understanding when code runs or is skipped helps avoid confusion and bugs.