Recall & Review
beginner
What is a macro in C?
A macro in C is a way to define a piece of code or a value that the compiler replaces before actual compilation. It is created using the #define directive.
Click to reveal answer
beginner
How do you define a simple macro for a constant value?
You use #define followed by the macro name and the value. For example: <br>
#define PI 3.14 defines PI as 3.14.Click to reveal answer
beginner
What happens when you use a macro in your code?
The preprocessor replaces every occurrence of the macro name with its defined value or code before the compiler runs.
Click to reveal answer
intermediate
How do you define a macro that takes arguments?
You define it like a function but with #define. For example:<br>
#define SQUARE(x) ((x) * (x)) defines a macro to square a number.Click to reveal answer
intermediate
Why should you use parentheses in macro definitions with arguments?
Parentheses ensure the correct order of operations when the macro is expanded. Without them, expressions can give wrong results.
Click to reveal answer
What does the #define directive do in C?
✗ Incorrect
The #define directive creates a macro that replaces text before the compiler processes the code.
How would you define a macro named MAX with value 100?
✗ Incorrect
Macros are defined using #define followed by the name and value.
What is the output of this code?<br>
#define DOUBLE(x) (2 * (x))<br>int a = DOUBLE(3 + 1);✗ Incorrect
With parentheses around x, DOUBLE(3 + 1) becomes (2 * (3 + 1)) = 8.
Why use parentheses around macro arguments in definitions?
✗ Incorrect
Parentheses ensure the macro arguments are evaluated correctly when expanded.
Which of these is a function-like macro?
✗ Incorrect
Function-like macros take arguments and look like functions but are replaced by the preprocessor.
Explain what a macro is in C and how it works.
Think about how the compiler sees your code after preprocessing.
You got /4 concepts.
Describe how to safely define a macro that takes arguments and why parentheses are important.
Consider what happens when you pass expressions as arguments.
You got /4 concepts.