A macro with arguments is a preprocessor directive that defines a reusable code snippet which can take inputs (arguments) and replace them in the code before compilation.
Click to reveal answer
beginner
How do you define a macro with two arguments to add them in C?
#define ADD(x, y) ((x) + (y))
This macro takes two arguments and returns their sum.
Click to reveal answer
intermediate
Why should you use parentheses around macro arguments and the whole macro body?
Parentheses ensure the correct order of operations and prevent unexpected results when the macro is used with complex expressions.
Click to reveal answer
intermediate
What happens if you forget to put parentheses around macro arguments?
The macro may produce wrong results due to operator precedence issues, for example, ADD(2, 3) * 4 could be expanded incorrectly.
Click to reveal answer
beginner
Give an example of a macro with arguments that calculates the maximum of two values.
#define MAX(a, b) ((a) > (b) ? (a) : (b))
This macro returns the larger of two values.
Click to reveal answer
What does this macro do? #define SQUARE(x) ((x) * (x))
ACalculates the square of x
BCalculates the square root of x
CMultiplies x by 2
DAdds x to itself
✗ Incorrect
The macro multiplies x by itself, which is the square of x.
Why is it important to put parentheses around macro arguments?
ATo avoid syntax errors
BTo make the macro faster
CTo ensure correct order of operations
DTo allow macros to take multiple arguments
✗ Incorrect
Parentheses ensure that expressions passed as arguments are evaluated correctly without precedence issues.
What will be the output of this code?
#define ADD(x, y) x + y
int result = ADD(2, 3) * 4;
A20
BUndefined behavior
CIncorrect code, will not compile
D14
✗ Incorrect
Without parentheses, ADD(2, 3) * 4 expands to 2 + 3 * 4 = 2 + 12 = 14, not (2 + 3) * 4 = 20.
How do you define a macro with three arguments?
A#define MACRO(a, b, c) ...
B#define MACRO(a b c) ...
C#define MACRO[a, b, c] ...
D#define MACRO{a, b, c} ...
✗ Incorrect
Macro arguments are listed in parentheses separated by commas.
Which of these is a correct macro to find the minimum of two numbers?
A#define MIN(a, b) (a < b ? a : b)
B#define MIN(a, b) ((a) < (b) ? (a) : (b))
C#define MIN(a, b) a < b ? a : b
D#define MIN(a, b) a < b ? (a) : (b)
✗ Incorrect
Option B uses parentheses around arguments and the whole expression to ensure correct evaluation.
Explain how to write a macro with arguments in C and why parentheses are important.
Think about how the preprocessor replaces code and how operator precedence can affect results.
You got /4 concepts.
Describe a real-life example where a macro with arguments can simplify your C code.
Consider simple math operations or comparisons you do often.
You got /4 concepts.
Practice
(1/5)
1. What does a macro with arguments in C do?
easy
A. It replaces code snippets with given values before compilation
B. It creates a new function at runtime
C. It allocates memory dynamically
D. It executes code conditionally during runtime
Solution
Step 1: Understand macro replacement
Macros with arguments are replaced by the preprocessor before the program runs, substituting the macro call with the code snippet using the given arguments.
Step 2: Differentiate from functions
Unlike functions, macros do not create executable code at runtime but perform textual substitution during compilation.
Final Answer:
It replaces code snippets with given values before compilation -> Option A
Quick Check:
Macro = code substitution before compile [OK]
Hint: Macros replace code before running, not at runtime [OK]
Common Mistakes:
Thinking macros create functions
Confusing macros with runtime code
Assuming macros allocate memory
2. Which of the following is the correct syntax for defining a macro with two arguments a and b that adds them?
easy
A. #define ADD(a, b) a + b
B. #define ADD a, b (a + b)
C. #define ADD(a, b) (a + b)
D. #define ADD(a, b) {a + b}
Solution
Step 1: Check macro argument syntax
Macro arguments must be enclosed in parentheses immediately after the macro name, like ADD(a, b).
Step 2: Ensure proper expression grouping
To avoid operator precedence issues, the expression should be enclosed in parentheses: (a + b).
Hint: Always put parentheses around macro arguments and whole expression [OK]
Common Mistakes:
Missing parentheses around arguments
Not enclosing expression in parentheses
Using braces {} instead of parentheses
3. Given the macro definition #define SQUARE(x) x * x, what is the output of printf("%d", SQUARE(3 + 1));?
medium
A. Error
B. 16
C. 12
D. 7
Solution
Step 1: Expand the macro with argument
The macro call SQUARE(3 + 1) expands to 3 + 1 * 3 + 1 because the macro definition lacks parentheses around the expression.
Step 2: Apply operator precedence
Multiplication has higher precedence, so 3 + (1 * 3) + 1 = 3 + 3 + 1 = 7.
Final Answer:
7 -> Option D
Quick Check:
Without parentheses, macro expands incorrectly = 7 [OK]
Hint: Without parentheses, macro args can cause wrong math [OK]
Common Mistakes:
Assuming macro adds parentheses automatically
Ignoring operator precedence
Expecting 16 as square of 4
4. Identify the error in this macro definition: #define MULTIPLY(a, b) a * b)
medium
A. Extra closing parenthesis at the end
B. Missing opening parenthesis before a
C. Arguments should not have parentheses
D. Macro name should be lowercase
Solution
Step 1: Check parentheses balance
The macro ends with an extra closing parenthesis ")" that does not match any opening parenthesis.
Step 2: Confirm correct macro syntax
Macro should be defined as #define MULTIPLY(a, b) (a * b) with balanced parentheses around the expression.
Final Answer:
Extra closing parenthesis at the end -> Option A
Quick Check:
Parentheses must be balanced in macro definitions [OK]
Hint: Count parentheses carefully in macro definitions [OK]
Common Mistakes:
Ignoring unmatched parentheses
Thinking arguments can't have parentheses
Believing macro names must be lowercase
5. You want to create a macro MAX(a, b) that returns the larger of two values. Which definition correctly handles expressions as arguments without errors?
hard
A. #define MAX(a, b) ((a > b) ? a : b)
B. #define MAX(a, b) ((a) > (b) ? (a) : (b))
C. #define MAX(a, b) a > b ? a : b
D. #define MAX(a, b) (a > b ? a : b)
Solution
Step 1: Understand argument safety
To safely handle expressions as arguments, each argument must be enclosed in parentheses to avoid precedence errors.
Step 2: Check full macro expression grouping
The entire conditional expression should be enclosed in parentheses to ensure correct evaluation when used in larger expressions.
Final Answer:
#define MAX(a, b) ((a) > (b) ? (a) : (b)) -> Option B
Quick Check:
Parentheses around args and whole expression prevent bugs [OK]
Hint: Always parenthesize macro args and whole expression [OK]
Common Mistakes:
Missing parentheses around arguments
Not enclosing entire expression
Assuming macro works like a function
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.