Conditional compilation lets you include or skip parts of your code when you build your program. This helps make your program work differently in different situations without changing the code every time.
Conditional compilation
#ifdef MACRO_NAME // code here #endif #ifndef MACRO_NAME // code here #endif #if EXPRESSION // code here #else // alternative code #endif
#ifdef checks if a macro is defined.
#ifndef checks if a macro is NOT defined.
DEBUG is defined.#define DEBUG
#ifdef DEBUG
printf("Debugging is ON\n");
#endifRELEASE is NOT defined.#ifndef RELEASE
printf("Not in release mode\n");
#endifVERSION.#if VERSION >= 2 printf("Version 2 or higher\n"); #else printf("Version less than 2\n"); #endif
This program uses conditional compilation to print messages based on defined macros. It shows how #ifdef, #if, and #ifndef work.
#include <stdio.h> #define DEBUG #define VERSION 3 int main() { #ifdef DEBUG printf("Debug mode is active\n"); #endif #if VERSION >= 2 printf("Version is 2 or higher\n"); #else printf("Version is less than 2\n"); #endif #ifndef RELEASE printf("Release mode is OFF\n"); #endif return 0; }
Macros used in conditional compilation are usually defined with #define or passed as compiler options.
Conditional compilation happens before the program runs, during the build process.
Use conditional compilation to keep your code clean and flexible.
Conditional compilation lets you include or exclude code when building your program.
Use #ifdef, #ifndef, and #if to check conditions.
This helps make your program adaptable to different needs without changing the source code each time.
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
