0
0
Cprogramming~20 mins

Macro with arguments - 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
Output of macro with arguments
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\n", result);
    return 0;
}
A7
B16
CError: macro expansion
D10
Attempts:
2 left
💡 Hint
Remember how macros expand without parentheses around arguments.
Predict Output
intermediate
2:00remaining
Correct macro usage with parentheses
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\n", result);
    return 0;
}
AError: macro expansion
B7
C10
D16
Attempts:
2 left
💡 Hint
Parentheses in macros help control operator precedence.
🔧 Debug
advanced
2:00remaining
Identify the error in macro with arguments
What error does this code produce when compiled?
C
#include <stdio.h>
#define MAX(a, b) a > b ? a : b

int main() {
    int x = 5, y = 10;
    int max_val = MAX(x, y);
    printf("%d\n", max_val);
    return 0;
}
APrints 5
BPrints 10
CSyntax error due to missing parentheses
DRuntime error
Attempts:
2 left
💡 Hint
Check how the macro expands and operator precedence.
Predict Output
advanced
2:00remaining
Macro side effects with arguments
What is the output of this C program?
C
#include <stdio.h>
#define INCREMENT(x) (x + 1)

int main() {
    int a = 5;
    int result = INCREMENT(a++) * 2;
    printf("%d %d\n", result, a);
    return 0;
}
A12 6
B14 7
C13 7
DUndefined behavior
Attempts:
2 left
💡 Hint
Macros can cause unexpected side effects when arguments have increments.
🧠 Conceptual
expert
2:00remaining
Why use parentheses in macro arguments?
Which option best explains why parentheses are important around macro arguments and the entire macro body?
ATo allow the macro to accept multiple arguments
BTo make the macro run faster at runtime
CTo ensure the macro expands to a single expression and respects operator precedence
DTo prevent the macro from being replaced by the preprocessor
Attempts:
2 left
💡 Hint
Think about how expressions combine when macros are expanded.