Bird
Raised Fist0
Cprogramming~5 mins

Define macro - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Define macro
O(1)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Single multiplication done by the macro.
  • How many times: Exactly once in this example.
How Execution Grows With Input

Since the macro just replaces code, the number of operations stays the same no matter the input size.

Input Size (n)Approx. Operations
101 multiplication
1001 multiplication
10001 multiplication

Pattern observation: The work does not increase with input size here.

Final Time Complexity

Time Complexity: O(1)

This means the program does a fixed amount of work regardless of input size.

Common Mistake

[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.

Interview Connect

Understanding how macros work helps you explain how code runs efficiently and how compile-time changes affect runtime.

Self-Check

What if we used the macro inside a loop that runs n times? How would the time complexity change?

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