0
0
Cprogramming~20 mins

Define macro - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Macro Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A16
B7
CError: macro expansion invalid
D10
Attempts:
2 left
💡 Hint
Remember how macros expand without parentheses around parameters.
Predict Output
intermediate
2: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;
}
AError: macro expansion invalid
B10
C16
D7
Attempts:
2 left
💡 Hint
Parentheses in macros help control the order of operations.
Predict Output
advanced
2: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;
}
AUndefined behavior or unexpected result
B5
C4
D6
Attempts:
2 left
💡 Hint
Macros can cause expressions to be evaluated multiple times.
🧠 Conceptual
advanced
1:30remaining
What does this macro do?
Given the macro below, what is its purpose?
C
#define MAX(a,b) ((a) > (b) ? (a) : (b))
AReturns the sum of a and b
BReturns the smaller of a and b
CReturns true if a equals b
DReturns the larger of a and b
Attempts:
2 left
💡 Hint
Look at the conditional operator ? :
🔧 Debug
expert
2: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;
}
ABecause macro expansion lacks parentheses causing wrong operator precedence
BBecause x++ cannot be used inside macros
CBecause the macro is missing a semicolon
DBecause multiplication operator * is invalid here
Attempts:
2 left
💡 Hint
Think about how the macro expands and operator precedence.