Conditional compilation - Time & Space Complexity
Conditional compilation controls which parts of code run based on conditions.
We want to see how this affects the time the program takes to run.
Analyze the time complexity of the following code snippet.
#ifdef FEATURE_ENABLED
void feature() {
for (int i = 0; i < n; i++) {
process(i);
}
}
#endif
int main() {
#ifdef FEATURE_ENABLED
feature();
#endif
return 0;
}
This code runs a loop only if FEATURE_ENABLED is defined during compilation.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A for-loop running from 0 to n-1 inside the feature() function.
- How many times: The loop runs n times if FEATURE_ENABLED is defined; otherwise, it does not run at all.
The loop runs once for each item up to n, so more input means more work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loop steps if enabled, 0 if not |
| 100 | 100 loop steps if enabled, 0 if not |
| 1000 | 1000 loop steps if enabled, 0 if not |
Pattern observation: The work grows directly with n only when the feature is included; otherwise, it stays zero.
Time Complexity: O(n)
This means the program's running time grows in a straight line with the input size when the feature is included.
[X] Wrong: "Conditional compilation changes the time complexity regardless of what code is included."
[OK] Correct: Conditional compilation only includes or excludes code before running, so the time complexity depends on the code that actually runs, not on the condition itself.
Understanding how conditional compilation affects which code runs helps you explain performance clearly and shows you know how code structure impacts efficiency.
"What if the loop inside feature() called another function that itself had a loop over n? How would the time complexity change?"
Practice
#ifdef directive do in C conditional compilation?Solution
Step 1: Understand
The#ifdefusage#ifdefdirective checks if a macro is defined.Step 2: Effect on code inclusion
If the macro is defined, the code inside the#ifdefblock is included during compilation.Final Answer:
Includes the code block only if the specified macro is defined. -> Option AQuick Check:
#ifdefmeans "if defined" [OK]
#ifdef means "if defined" [OK]- Confusing
#ifdefwith#ifndef - Thinking it always includes code
- Assuming it excludes code if macro is defined
DEBUG is NOT defined?Solution
Step 1: Identify the directive for 'not defined'
The directive#ifndefmeans "if not defined".Step 2: Match with macro
UsingDEBUG#ifndef DEBUGchecks ifDEBUGis not defined.Final Answer:
#ifndef DEBUG -> Option BQuick Check:
#ifndefmeans "if not defined" [OK]
#ifndef to check if macro is NOT defined [OK]- Using
#ifdefinstead of#ifndef - Using
#if !DEBUGwhich is invalid for macro existence - Confusing
#ifwith#ifndef
FEATURE is defined?
#include <stdio.h>
#define FEATURE
int main() {
#ifdef FEATURE
printf("Feature is enabled\n");
#else
printf("Feature is disabled\n");
#endif
return 0;
}Solution
Step 1: Check if
The code hasFEATUREis defined#define FEATURE, soFEATUREis defined.Step 2: Evaluate
Since#ifdef FEATUREblockFEATUREis defined, the code inside#ifdef FEATUREruns, printing "Feature is enabled".Final Answer:
Feature is enabled -> Option AQuick Check:
Defined macro triggers#ifdefblock [OK]
#ifdef block runs [OK]- Ignoring the #define line
- Thinking
#elseblock runs - Assuming compilation error due to macro
#include <stdio.h>
int main() {
#ifdef DEBUG
printf("Debug mode\n");
#else
printf("Release mode\n");
#endif
return 0;
}Solution
Step 1: Check code structure
The code opensint main() {but does not have a closing}brace.Step 2: Verify directives and syntax
The#ifdefand#elsedirectives are correctly used; missing#define DEBUGis not an error, just changes output.Final Answer:
Missing closing brace for main function -> Option DQuick Check:
Every function needs matching braces [OK]
- Assuming missing #define is an error
- Thinking #ifdef usage is wrong
- Ignoring missing closing brace
DEBUG is defined, otherwise it prints "Normal mode". Which code snippet correctly achieves this using conditional compilation?Solution
Step 1: Understand the requirement
Print "Debug mode" only ifDEBUGis defined, else print "Normal mode".Step 2: Choose correct directive
#ifdef DEBUGchecks ifDEBUGis defined and includes the first print statement;#elsehandles the other case.Final Answer:
#ifdef DEBUG printf("Debug mode\n"); #else printf("Normal mode\n"); #endif -> Option CQuick Check:
Use#ifdeffor defined macro check [OK]
#ifdef for defined, #else for else [OK]- Using
#ifndefwhich reverses logic - Using
#if DEBUGwithout macro value - Using
#if !DEBUGwhich is invalid
Ready to prove it?
Pass the test and this pattern moves unknown
