Recall & Review
beginner
What is a macro with arguments in C?
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))
✗ Incorrect
The macro multiplies x by itself, which is the square of x.
Why is it important to put parentheses around macro 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;
✗ 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?
✗ Incorrect
Macro arguments are listed in parentheses separated by commas.
Which of these is a correct macro to find the minimum of two numbers?
✗ 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.