Challenge - 5 Problems
Macro Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this macro usage?
Consider the following C code using a macro. What will be printed when this program runs?
C
#include <stdio.h> #define SQUARE(x) x * x int main() { int a = 3; printf("%d\n", SQUARE(a + 1)); return 0; }
Attempts:
2 left
💡 Hint
Remember how macros expand without parentheses around parameters.
✗ Incorrect
The macro expands to a + 1 * a + 1. With a=3, due to operator precedence (* before +), it evaluates to 3 + (1 * 3) + 1 = 3 + 3 + 1 = 7.
❓ Predict Output
intermediate2:00remaining
What is the output of this corrected macro?
Now consider this corrected macro definition. What will be printed?
C
#include <stdio.h> #define SQUARE(x) ((x) * (x)) int main() { int a = 3; printf("%d\n", SQUARE(a + 1)); return 0; }
Attempts:
2 left
💡 Hint
Parentheses in macros help control the order of operations.
✗ Incorrect
The macro expands to ((a + 1) * (a + 1)) which is (4 * 4) = 16.
❓ Predict Output
advanced2:00remaining
What is the output of this macro with side effects?
Look at this code using a macro that doubles a value. What will be printed?
C
#include <stdio.h> #define DOUBLE(x) ((x) + (x)) int main() { int i = 2; printf("%d\n", DOUBLE(i++)); return 0; }
Attempts:
2 left
💡 Hint
Macros can cause expressions to be evaluated multiple times.
✗ Incorrect
The macro expands to ((i++) + (i++)), so i++ is evaluated twice, causing side effects and undefined or unexpected results.
🧠 Conceptual
advanced1:30remaining
What does this macro do?
Given the macro below, what is its purpose?
C
#define MAX(a,b) ((a) > (b) ? (a) : (b))
Attempts:
2 left
💡 Hint
Look at the conditional operator ? :
✗ Incorrect
The macro compares a and b and returns the one that is greater.
🔧 Debug
expert2:30remaining
Why does this macro cause a compilation error?
Examine the macro and code below. Why does it cause a compilation error?
C
#define INCREMENT(x) x++ int main() { int a = 5; int b = INCREMENT(a) * 2; return 0; }
Attempts:
2 left
💡 Hint
Think about how the macro expands and operator precedence.
✗ Incorrect
The macro expands to a++ * 2 which is parsed as (a++) * 2, but without parentheses around x++, it can cause unexpected behavior or warnings. The main issue is lack of parentheses causing operator precedence problems.