Macros with arguments let you create reusable code pieces that work like small functions but run faster because they replace code directly.
Macro with arguments
#define MACRO_NAME(argument1, argument2, ...) (expression using arguments)
Macros are replaced by the preprocessor before the program runs.
Arguments in macros are like placeholders that get replaced with actual values.
#define SQUARE(x) ((x) * (x))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ADD(x, y) ((x) + (y))
This program uses a macro with arguments to multiply two numbers and print the result.
#include <stdio.h> #define MULTIPLY(a, b) ((a) * (b)) int main() { int x = 5; int y = 3; int result = MULTIPLY(x, y); printf("%d times %d is %d\n", x, y, result); return 0; }
Always use parentheses around macro arguments and the whole expression to avoid unexpected results.
Macros do not check types, so be careful with side effects like incrementing variables inside arguments.
Macros are replaced before compilation, so debugging can be harder than with functions.
Macros with arguments let you write reusable code snippets that work like simple functions.
They are replaced directly in your code before it runs, making them fast but sometimes tricky.
Use parentheses carefully to avoid mistakes in calculations.
Practice
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 AQuick Check:
Macro = code substitution before compile [OK]
- Thinking macros create functions
- Confusing macros with runtime code
- Assuming macros allocate memory
a and b that adds them?Solution
Step 1: Check macro argument syntax
Macro arguments must be enclosed in parentheses immediately after the macro name, likeADD(a, b).Step 2: Ensure proper expression grouping
To avoid operator precedence issues, the expression should be enclosed in parentheses:(a + b).Final Answer:
#define ADD(a, b) (a + b) -> Option CQuick Check:
Correct macro syntax = #define NAME(args) (expression) [OK]
- Missing parentheses around arguments
- Not enclosing expression in parentheses
- Using braces {} instead of parentheses
#define SQUARE(x) x * x, what is the output of printf("%d", SQUARE(3 + 1));?Solution
Step 1: Expand the macro with argument
The macro callSQUARE(3 + 1)expands to3 + 1 * 3 + 1because the macro definition lacks parentheses around the expression.Step 2: Apply operator precedence
Multiplication has higher precedence, so3 + (1 * 3) + 1 = 3 + 3 + 1 = 7.Final Answer:
7 -> Option DQuick Check:
Without parentheses, macro expands incorrectly = 7 [OK]
- Assuming macro adds parentheses automatically
- Ignoring operator precedence
- Expecting 16 as square of 4
#define MULTIPLY(a, b) a * b)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 AQuick Check:
Parentheses must be balanced in macro definitions [OK]
- Ignoring unmatched parentheses
- Thinking arguments can't have parentheses
- Believing macro names must be lowercase
MAX(a, b) that returns the larger of two values. Which definition correctly handles expressions as arguments without errors?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 BQuick Check:
Parentheses around args and whole expression prevent bugs [OK]
- Missing parentheses around arguments
- Not enclosing entire expression
- Assuming macro works like a function
Ready to prove it?
Pass the test and this pattern moves unknown
