Bird
Raised Fist0
Cprogramming~20 mins

Conditional compilation - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Conditional Compilation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of conditional compilation with defined macro
What is the output of this C program when compiled and run?
C
#include <stdio.h>

#define DEBUG

int main() {
#ifdef DEBUG
    printf("Debug mode\n");
#else
    printf("Release mode\n");
#endif
    return 0;
}
ARelease mode
BDebug mode
CCompilation error
DNo output
Attempts:
2 left
💡 Hint
Look at the #ifdef directive and the defined macro.
❓ Predict Output
intermediate
2:00remaining
Output when macro is not defined
What will this program print when compiled and run?
C
#include <stdio.h>

int main() {
#ifndef RELEASE
    printf("Debug build\n");
#else
    printf("Release build\n");
#endif
    return 0;
}
ACompilation error
BRelease build
CDebug build
DNo output
Attempts:
2 left
💡 Hint
Check if RELEASE is defined or not.
❓ Predict Output
advanced
2:00remaining
Output with nested conditional compilation
What is the output of this program?
C
#include <stdio.h>

#define FEATURE_X

int main() {
#ifdef FEATURE_X
    #ifdef FEATURE_Y
        printf("Feature X and Y enabled\n");
    #else
        printf("Only Feature X enabled\n");
    #endif
#else
    printf("No features enabled\n");
#endif
    return 0;
}
AOnly Feature X enabled
BFeature X and Y enabled
CCompilation error
DNo features enabled
Attempts:
2 left
💡 Hint
Check which macros are defined.
❓ Predict Output
advanced
2:00remaining
Output with macro value comparison
What will this program print when compiled and run?
C
#include <stdio.h>

#define VERSION 3

int main() {
#if VERSION >= 5
    printf("Version 5 or higher\n");
#elif VERSION == 3
    printf("Version 3 detected\n");
#else
    printf("Older version\n");
#endif
    return 0;
}
ACompilation error
BVersion 5 or higher
COlder version
DVersion 3 detected
Attempts:
2 left
💡 Hint
Look at the value of VERSION and the #if conditions.
❓ Predict Output
expert
3:00remaining
Output with complex conditional compilation and macro undefinition
What is the output of this program?
C
#include <stdio.h>

#define MODE 1

#if MODE == 1
    #define FEATURE_A
#elif MODE == 2
    #define FEATURE_B
#endif

#undef MODE

int main() {
#ifdef FEATURE_A
    printf("Feature A active\n");
#endif
#ifdef FEATURE_B
    printf("Feature B active\n");
#endif
#ifdef MODE
    printf("Mode defined\n");
#else
    printf("Mode undefined\n");
#endif
    return 0;
}
A
Feature A active
Mode undefined
B
Feature B active
Mode undefined
C
Feature A active
Mode defined
DCompilation error
Attempts:
2 left
💡 Hint
Check which macros are defined and which are undefined after #undef.

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

  1. Step 1: Understand #ifdef usage

    The #ifdef directive checks if a macro is defined.
  2. Step 2: Effect on code inclusion

    If the macro is defined, the code inside the #ifdef block is included during compilation.
  3. Final Answer:

    Includes the code block only if the specified macro is defined. -> Option A
  4. 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

  1. Step 1: Identify the directive for 'not defined'

    The directive #ifndef means "if not defined".
  2. Step 2: Match with macro DEBUG

    Using #ifndef DEBUG checks if DEBUG is not defined.
  3. Final Answer:

    #ifndef DEBUG -> Option B
  4. 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

  1. Step 1: Check if FEATURE is defined

    The code has #define FEATURE, so FEATURE is defined.
  2. Step 2: Evaluate #ifdef FEATURE block

    Since FEATURE is defined, the code inside #ifdef FEATURE runs, printing "Feature is enabled".
  3. Final Answer:

    Feature is enabled -> Option A
  4. Quick Check:

    Defined macro triggers #ifdef block [OK]
Hint: If macro defined, #ifdef block runs [OK]
Common Mistakes:
  • Ignoring the #define line
  • Thinking #else block runs
  • Assuming compilation error due to macro
4. Identify the error in this code snippet:
#include <stdio.h>

int main() {
#ifdef DEBUG
    printf("Debug mode\n");
#else
    printf("Release mode\n");
#endif
    return 0;
}
medium
A. printf statements are invalid
B. Incorrect use of #ifdef directive
C. Missing #define DEBUG before #ifdef
D. Missing closing brace for main function

Solution

  1. Step 1: Check code structure

    The code opens int main() { but does not have a closing } brace.
  2. Step 2: Verify directives and syntax

    The #ifdef and #else directives are correctly used; missing #define DEBUG is not an error, just changes output.
  3. Final Answer:

    Missing closing brace for main function -> Option D
  4. 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

  1. Step 1: Understand the requirement

    Print "Debug mode" only if DEBUG is defined, else print "Normal mode".
  2. Step 2: Choose correct directive

    #ifdef DEBUG checks if DEBUG is defined and includes the first print statement; #else handles the other case.
  3. Final Answer:

    #ifdef DEBUG printf("Debug mode\n"); #else printf("Normal mode\n"); #endif -> Option C
  4. Quick Check:

    Use #ifdef for defined macro check [OK]
Hint: Use #ifdef for defined, #else for else [OK]
Common Mistakes:
  • Using #ifndef which reverses logic
  • Using #if DEBUG without macro value
  • Using #if !DEBUG which is invalid
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard