Bird
Raised Fist0
Cprogramming~10 mins

Define macro - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Define macro
Start
↓
Preprocessor reads #define
↓
Store macro name and replacement
↓
Replace macro name with replacement in code
↓
Compile replaced code
↓
Run program
The C preprocessor reads the #define directive, stores the macro, replaces occurrences in code, then compilation and execution happen.
Execution Sample
C
#define PI 3.14
float area = PI * r * r;
Defines a macro PI as 3.14 and uses it to calculate area.
Execution Table
StepCode LineActionResulting Code
1#define PI 3.14Store macro PI with value 3.14No code output, macro stored
2float area = PI * r * r;Replace PI with 3.14float area = 3.14 * r * r;
3Compile replaced codeCompile float area = 3.14 * r * r;Ready to run
4Run programCalculate area using 3.14Area computed using 3.14
💡 All macros replaced, code compiled and executed
Variable Tracker
VariableStartAfter Macro ReplacementFinal
PIundefined3.143.14
areaundefinedundefinedcalculated value at runtime
Key Moments - 2 Insights
Why does the macro replacement happen before compilation?
Because the preprocessor runs first and replaces all macro names with their values before the compiler sees the code, as shown in execution_table step 2.
Is PI a variable that takes memory?
No, PI is just text replaced by 3.14 before compilation, so it does not use memory like a variable. See variable_tracker where PI is replaced.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2, what does the code line become after macro replacement?
Afloat area = PI * r * r;
Bfloat area = 3 * r * r;
Cfloat area = 3.14 * r * r;
Dfloat area = r * r;
💡 Hint
Check the 'Resulting Code' column in step 2 of execution_table
At which step does the macro get stored by the preprocessor?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table step 1
If we change #define PI 3.14 to #define PI 3.14159, what changes in the execution_table?
AStep 1 macro storage changes to PI = 3.14
BStep 2 code becomes float area = 3.14159 * r * r;
CStep 3 compilation fails
DNo change at all
💡 Hint
Macro replacement in step 2 depends on the macro value stored in step 1
Concept Snapshot
#define MACRO_NAME replacement
- Defines a macro for text replacement
- Preprocessor replaces MACRO_NAME with replacement before compilation
- Macros do not use memory
- Useful for constants or code snippets
Full Transcript
In C, #define creates a macro that replaces text before compilation. The preprocessor reads the #define line, stores the macro name and its replacement text. Then, wherever the macro name appears in code, it is replaced by the replacement text. This happens before the compiler runs. For example, #define PI 3.14 replaces all PI with 3.14. Macros are not variables and do not take memory. The execution table shows storing the macro, replacing it in code, compiling, and running. This helps write readable and maintainable code.

Practice

(1/5)
1. What does the #define directive do in C?
easy
A. It creates a macro that replaces text before compilation.
B. It declares a variable with a fixed value.
C. It defines a function that runs at runtime.
D. It allocates memory for a constant.

Solution

  1. Step 1: Understand the purpose of #define

    The #define directive tells the compiler to replace all occurrences of a name with a given value or code snippet before the program runs.
  2. Step 2: Differentiate from variables and functions

    Unlike variables or functions, macros do not allocate memory or run at runtime; they are simple text replacements done before compilation.
  3. Final Answer:

    It creates a macro that replaces text before compilation. -> Option A
  4. Quick Check:

    #define = text replacement [OK]
Hint: Macros replace text before running code [OK]
Common Mistakes:
  • Thinking macros allocate memory
  • Confusing macros with functions
  • Assuming macros run at runtime
2. Which of the following is the correct syntax to define a macro named PI with value 3.14?
easy
A. #define PI 3.14
B. #define PI = 3.14
C. define PI 3.14
D. #define(PI, 3.14)

Solution

  1. Step 1: Recall correct #define syntax

    The correct syntax is #define NAME value without an equal sign or parentheses.
  2. Step 2: Check each option

    #define PI = 3.14 uses an equal sign which is incorrect. define PI 3.14 misses the #. #define(PI, 3.14) uses parentheses incorrectly. Only #define PI 3.14 matches the correct syntax.
  3. Final Answer:

    #define PI 3.14 -> Option A
  4. Quick Check:

    Correct macro syntax = #define NAME value [OK]
Hint: No equal sign in macro definitions [OK]
Common Mistakes:
  • Adding '=' sign after macro name
  • Omitting '#' in #define
  • Using parentheses like a function
3. What is the output of this C code?
#include <stdio.h>
#define SQUARE(x) ((x) * (x))

int main() {
    int a = 3;
    printf("%d", SQUARE(a + 1));
    return 0;
}
medium
A. Error
B. 7
C. 10
D. 16

Solution

  1. Step 1: Expand the macro with argument

    The macro call SQUARE(a + 1) expands to ((a + 1) * (a + 1)).
  2. Step 2: Calculate the expression

    Substitute a = 3: ((3 + 1) * (3 + 1)) = (4 * 4) = 16.
  3. Final Answer:

    16 -> Option D
  4. Quick Check:

    Macro expansion and calculation = 16 [OK]
Hint: Remember to expand macros fully before calculating [OK]
Common Mistakes:
  • Not using parentheses in macro causing wrong order
  • Calculating as 3 + 1 * 3 + 1 = 7
  • Assuming macro is a function call
4. Identify the error in this macro definition:
#define MAX(a, b) a > b ? a : b
medium
A. Macro names cannot have parameters
B. Incorrect use of ? operator
C. Missing parentheses around the entire macro expression
D. Missing semicolon at the end

Solution

  1. Step 1: Analyze macro expression grouping

    The macro expands to a > b ? a : b without parentheses, which can cause wrong evaluation when used in expressions.
  2. Step 2: Understand importance of parentheses

    Wrapping the entire macro in parentheses like ((a) > (b) ? (a) : (b)) ensures correct order and prevents bugs.
  3. Final Answer:

    Missing parentheses around the entire macro expression -> Option C
  4. Quick Check:

    Always parenthesize macro expressions [OK]
Hint: Wrap macro expressions in parentheses to avoid errors [OK]
Common Mistakes:
  • Not using parentheses around macro body
  • Assuming macros behave like functions
  • Adding semicolon inside macro definition
5. You want to create a macro MIN that returns the smaller of two values x and y. Which of the following is the best macro definition to avoid common bugs?
hard
A. #define MIN(x, y) ((x < y) ? x : y)
B. #define MIN(x, y) ((x) < (y) ? (x) : (y))
C. #define MIN(x, y) (x < y ? x : y)
D. #define MIN(x, y) x < y ? x : y

Solution

  1. Step 1: Check for parentheses around parameters

    #define MIN(x, y) ((x) < (y) ? (x) : (y)) wraps each parameter in parentheses to avoid issues when arguments are expressions.
  2. Step 2: Check for parentheses around entire expression

    #define MIN(x, y) ((x) < (y) ? (x) : (y)) also wraps the whole expression in parentheses to ensure correct evaluation in larger expressions.
  3. Step 3: Compare with other options

    Options A, B, and C miss some parentheses, risking wrong results or precedence bugs.
  4. Final Answer:

    #define MIN(x, y) ((x) < (y) ? (x) : (y)) -> Option B
  5. Quick Check:

    Parenthesize parameters and expression in macros [OK]
Hint: Always parenthesize macro parameters and full expression [OK]
Common Mistakes:
  • Missing parentheses around parameters
  • Missing parentheses around entire macro
  • Assuming macros behave like functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard