Define macro - Time & Space Complexity
Let's see how using a macro affects the time it takes for a program to run.
We want to know how the program's steps grow when using a macro.
Analyze the time complexity of the following code snippet.
#include <stdio.h>
#define SQUARE(x) ((x) * (x))
int main() {
int n = 5;
int result = SQUARE(n);
printf("%d\n", result);
return 0;
}
This code defines a macro to square a number and uses it once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single multiplication done by the macro.
- How many times: Exactly once in this example.
Since the macro just replaces code, the number of operations stays the same no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 multiplication |
| 100 | 1 multiplication |
| 1000 | 1 multiplication |
Pattern observation: The work does not increase with input size here.
Time Complexity: O(1)
This means the program does a fixed amount of work regardless of input size.
[X] Wrong: "Macros slow down the program because they add extra steps at runtime."
[OK] Correct: Macros are replaced before running, so they don't add runtime steps themselves.
Understanding how macros work helps you explain how code runs efficiently and how compile-time changes affect runtime.
What if we used the macro inside a loop that runs n times? How would the time complexity change?
Practice
#define directive do in C?Solution
Step 1: Understand the purpose of
The#define#definedirective tells the compiler to replace all occurrences of a name with a given value or code snippet before the program runs.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.Final Answer:
It creates a macro that replaces text before compilation. -> Option AQuick Check:
#define= text replacement [OK]
- Thinking macros allocate memory
- Confusing macros with functions
- Assuming macros run at runtime
PI with value 3.14?Solution
Step 1: Recall correct
The correct syntax is#definesyntax#define NAME valuewithout an equal sign or parentheses.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.Final Answer:
#define PI 3.14 -> Option AQuick Check:
Correct macro syntax =#define NAME value[OK]
- Adding '=' sign after macro name
- Omitting '#' in #define
- Using parentheses like a function
#include <stdio.h>
#define SQUARE(x) ((x) * (x))
int main() {
int a = 3;
printf("%d", SQUARE(a + 1));
return 0;
}Solution
Step 1: Expand the macro with argument
The macro callSQUARE(a + 1)expands to((a + 1) * (a + 1)).Step 2: Calculate the expression
Substitutea = 3: ((3 + 1) * (3 + 1)) = (4 * 4) = 16.Final Answer:
16 -> Option DQuick Check:
Macro expansion and calculation = 16 [OK]
- Not using parentheses in macro causing wrong order
- Calculating as 3 + 1 * 3 + 1 = 7
- Assuming macro is a function call
#define MAX(a, b) a > b ? a : b
Solution
Step 1: Analyze macro expression grouping
The macro expands toa > b ? a : bwithout parentheses, which can cause wrong evaluation when used in expressions.Step 2: Understand importance of parentheses
Wrapping the entire macro in parentheses like((a) > (b) ? (a) : (b))ensures correct order and prevents bugs.Final Answer:
Missing parentheses around the entire macro expression -> Option CQuick Check:
Always parenthesize macro expressions [OK]
- Not using parentheses around macro body
- Assuming macros behave like functions
- Adding semicolon inside macro definition
MIN that returns the smaller of two values x and y. Which of the following is the best macro definition to avoid common bugs?Solution
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.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.Step 3: Compare with other options
Options A, B, and C miss some parentheses, risking wrong results or precedence bugs.Final Answer:
#define MIN(x, y) ((x) < (y) ? (x) : (y)) -> Option BQuick Check:
Parenthesize parameters and expression in macros [OK]
- Missing parentheses around parameters
- Missing parentheses around entire macro
- Assuming macros behave like functions
Ready to prove it?
Pass the test and this pattern moves unknown
