0
0
Cprogramming~20 mins

Conditional compilation - Practice Problems & Coding Challenges

Choose your learning style9 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.