Challenge - 5 Problems
Preprocessor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Purpose of the C Preprocessor
What is the main purpose of the C preprocessor in a C program?
Attempts:
2 left
💡 Hint
Think about what happens before the actual compilation starts.
✗ Incorrect
The C preprocessor runs before compilation and handles directives like #include and #define to prepare the source code.
🧠 Conceptual
intermediate2:00remaining
Common Uses of the Preprocessor
Which of the following is NOT a common use of the C preprocessor?
Attempts:
2 left
💡 Hint
Remember what the preprocessor can and cannot do.
✗ Incorrect
Memory allocation is done at runtime by functions like malloc, not by the preprocessor.
❓ Predict Output
advanced2:00remaining
Output of Preprocessor Macro Expansion
What is the output of this C program?
C
#include <stdio.h> #define SQUARE(x) x * x int main() { int a = 3; int result = SQUARE(a + 1); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Look at how the macro expands and operator precedence.
✗ Incorrect
The macro expands to a + 1 * a + 1 which is 3 + 1 * 3 + 1 = 3 + 3 + 1 = 7. The macro is SQUARE(x) x * x, so SQUARE(a + 1) becomes a + 1 * a + 1 without parentheses. Multiplication has higher precedence, so it is a + (1 * a) + 1 = 3 + (1*3) + 1 = 3 + 3 + 1 = 7. So output is 7.
❓ Predict Output
advanced2:00remaining
Conditional Compilation Output
What will be printed when this C code is 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; }
Attempts:
2 left
💡 Hint
Check if the DEBUG macro is defined or not.
✗ Incorrect
Since DEBUG is defined, the code inside #ifdef DEBUG runs, printing 'Debug mode'.
🧠 Conceptual
expert2:00remaining
Why Use the Preprocessor for Constants?
Why might a C programmer prefer using #define for constants instead of variables?
Attempts:
2 left
💡 Hint
Think about when the replacement happens for #define.
✗ Incorrect
#define constants are replaced by the preprocessor before compilation, so no memory is allocated for them.