What if your program could magically change itself to fit any device without extra work?
Why Conditional compilation? - Purpose & Use Cases
Imagine you are writing a program that needs to run on different devices, like a phone and a computer. You try to write separate versions of the code for each device by hand, copying and changing many parts manually.
This manual way is slow and confusing. You might forget to change some parts, causing bugs. Also, updating the program means changing many copies, which wastes time and can cause mistakes.
Conditional compilation lets you write one code file with special instructions that include or skip parts depending on the device or situation. The computer picks the right parts automatically when building the program.
#ifdef PHONE // phone code #endif #ifdef COMPUTER // computer code #endif
#if defined(PHONE) // phone code #elif defined(COMPUTER) // computer code #endif
It makes your code flexible and easy to maintain by automatically choosing the right parts for each device or condition.
For example, a game might use conditional compilation to include special graphics for powerful computers but simpler graphics for phones, all from the same code base.
Manual copying of code for different cases is slow and error-prone.
Conditional compilation lets the computer pick code parts automatically.
This keeps code clean, flexible, and easier to update.
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
