Conditional compilation allows the compiler to include or exclude parts of the code based on certain conditions, usually using preprocessor directives like #if, #ifdef, and #ifndef.
Click to reveal answer
beginner
What does the #ifdef directive do?
#ifdef checks if a macro is defined. If it is, the code following it up to #endif is included in compilation.
Click to reveal answer
beginner
How do you exclude code when a macro is defined?
Use #ifndef MACRO_NAME to include code only if the macro is NOT defined.
Click to reveal answer
beginner
What is the purpose of #else in conditional compilation?
#else provides an alternative block of code to compile if the condition in #if or #ifdef is false.
Click to reveal answer
intermediate
Give an example of conditional compilation to compile code only on Windows.
Example:
#ifdef _WIN32
// Windows-specific code
#endif
This compiles the code inside only if _WIN32 is defined, which usually means the program is being compiled on Windows.
Click to reveal answer
Which directive checks if a macro is NOT defined?
A#else
B#ifndef
C#if
D#ifdef
✗ Incorrect
#ifndef means "if not defined" and includes code only if the macro is not defined.
What happens to code inside #ifdef MACRO if MACRO is not defined?
AIt is excluded from compilation
BIt is always compiled
CIt causes a compilation error
DIt is compiled twice
✗ Incorrect
If the macro is not defined, the code inside #ifdef is skipped.
Which directive can be used to provide an alternative code block if a condition is false?
A#include
B#endif
C#define
D#else
✗ Incorrect
#else is used to specify code that compiles when the #if or #ifdef condition is false.
What is the role of #endif in conditional compilation?
AIt includes a file
BIt defines a macro
CIt ends a conditional directive block
DIt comments out code
✗ Incorrect
#endif marks the end of a conditional compilation block started by #if, #ifdef, or #ifndef.
Which directive would you use to check a condition with a value, not just if a macro is defined?
A#if
B#ifdef
C#ifndef
D#else
✗ Incorrect
#if allows checking expressions or values, unlike #ifdef which only checks if a macro is defined.
Explain how conditional compilation helps in writing cross-platform C programs.
Think about how you can write code that works on Windows and Linux by including only the right parts.
You got /3 concepts.
Describe the difference between #ifdef and #if directives.
One checks presence, the other checks a condition.
You got /3 concepts.
Practice
(1/5)
1. What does the #ifdef directive do in C conditional compilation?
easy
A. Includes the code block only if the specified macro is defined.
B. Includes the code block only if the specified macro is not defined.
C. Always includes the code block regardless of any macro.
D. Excludes the code block if the specified macro is defined.
Solution
Step 1: Understand #ifdef usage
The #ifdef directive checks if a macro is defined.
Step 2: Effect on code inclusion
If the macro is defined, the code inside the #ifdef block is included during compilation.
Final Answer:
Includes the code block only if the specified macro is defined. -> Option A
Quick Check:
#ifdef means "if defined" [OK]
Hint: Remember: #ifdef means "if defined" [OK]
Common Mistakes:
Confusing #ifdef with #ifndef
Thinking it always includes code
Assuming it excludes code if macro is defined
2. Which of the following is the correct syntax to check if a macro DEBUG is NOT defined?
easy
A. #ifdef DEBUG
B. #ifndef DEBUG
C. #if DEBUG
D. #if !DEBUG
Solution
Step 1: Identify the directive for 'not defined'
The directive #ifndef means "if not defined".
Step 2: Match with macro DEBUG
Using #ifndef DEBUG checks if DEBUG is not defined.
Final Answer:
#ifndef DEBUG -> Option B
Quick Check:
#ifndef means "if not defined" [OK]
Hint: Use #ifndef to check if macro is NOT defined [OK]
Common Mistakes:
Using #ifdef instead of #ifndef
Using #if !DEBUG which is invalid for macro existence
Confusing #if with #ifndef
3. What will be the output of this code if 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;
}
medium
A. Feature is enabled
B. Feature is disabled
C. Compilation error
D. No output
Solution
Step 1: Check if FEATURE is defined
The code has #define FEATURE, so FEATURE is defined.
Step 2: Evaluate #ifdef FEATURE block
Since FEATURE is defined, the code inside #ifdef FEATURE runs, printing "Feature is enabled".
The code opens int main() { but does not have a closing } brace.
Step 2: Verify directives and syntax
The #ifdef and #else directives are correctly used; missing #define DEBUG is not an error, just changes output.
Final Answer:
Missing closing brace for main function -> Option D
Quick Check:
Every function needs matching braces [OK]
Hint: Check all braces match in code blocks [OK]
Common Mistakes:
Assuming missing #define is an error
Thinking #ifdef usage is wrong
Ignoring missing closing brace
5. You want to compile a program that prints "Debug mode" only if the macro DEBUG is defined, otherwise it prints "Normal mode". Which code snippet correctly achieves this using conditional compilation?
hard
A. #if !DEBUG
printf("Debug mode\n");
#else
printf("Normal mode\n");
#endif
B. #ifndef DEBUG
printf("Debug mode\n");
#else
printf("Normal mode\n");
#endif
C. #ifdef DEBUG
printf("Debug mode\n");
#else
printf("Normal mode\n");
#endif
D. #if DEBUG
printf("Debug mode\n");
#else
printf("Normal mode\n");
#endif
Solution
Step 1: Understand the requirement
Print "Debug mode" only if DEBUG is defined, else print "Normal mode".
Step 2: Choose correct directive
#ifdef DEBUG checks if DEBUG is defined and includes the first print statement; #else handles the other case.