0
0
Cprogramming~10 mins

Macro with arguments - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Macro with arguments
Define macro with args
Use macro in code
Preprocessor replaces macro call
Code compiles with replaced text
Program runs with macro effect
A macro with arguments is defined and then used in code; the preprocessor replaces the macro call with the expanded code before compilation.
Execution Sample
C
#define SQUARE(x) ((x) * (x))
int main() {
  int a = 5;
  int b = SQUARE(a);
  return b;
}
This code defines a macro SQUARE that calculates the square of a number and uses it to compute b = 25.
Execution Table
StepCode LineMacro ExpansionVariable ValuesAction
1#define SQUARE(x) ((x) * (x))Macro defined-Macro SQUARE with argument x is defined
2int a = 5;-a=5Variable a initialized to 5
3int b = SQUARE(a);int b = ((a) * (a));a=5, b=25Macro call replaced by ((a) * (a)), b computed as 25
4return b;-b=25Return value is 25
5Program ends--Program ends with return 25
💡 Program ends after returning b=25, macro expansion done before compilation
Variable Tracker
VariableStartAfter Step 2After Step 3Final
aundefined555
bundefinedundefined2525
Key Moments - 2 Insights
Why does the macro use extra parentheses around (x) and the whole expression?
Parentheses ensure correct order of operations when the macro argument is an expression; see Step 3 where ((a) * (a)) prevents errors if x is complex.
Is the macro a function? Does it evaluate its argument multiple times?
No, it's a text replacement done before compilation. The argument x is repeated twice in the expansion, so if x has side effects, it runs twice. See Step 3 macro expansion.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at Step 3, what is the value of b after macro expansion?
A5
B25
C10
DUndefined
💡 Hint
Check the 'Variable Values' column at Step 3 in the execution table
At which step is the macro SQUARE actually replaced by its code?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Macro Expansion' column to see where replacement happens
If we change the macro to #define SQUARE(x) (x * x) without parentheses, what problem might occur?
ANo problem, works same
BCompilation error
CWrong calculation if x is an expression like a+1
DMacro won't expand
💡 Hint
Think about operator precedence and how parentheses affect the expanded code
Concept Snapshot
#define MACRO_NAME(args) replacement
Macro with arguments replaces calls with code using those args.
Use parentheses around args and whole expression to avoid errors.
Macro is text substitution done before compilation.
Beware: arguments may be evaluated multiple times.
Full Transcript
This example shows how a macro with arguments works in C. First, the macro SQUARE is defined to compute the square of a value. When the program runs, the macro call SQUARE(a) is replaced by ((a) * (a)) before compilation. Variable a is set to 5, then b is assigned the result of the macro expansion, which is 25. The program returns 25. Parentheses in the macro ensure correct calculation even if the argument is complex. The macro is not a function; it is a text replacement, so arguments can be evaluated multiple times if used more than once in the macro body.