0
0
Cprogramming~5 mins

Predefined macros - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Predefined macros
O(1)
Understanding Time Complexity

Let's see how using predefined macros affects the speed of a C program.

We want to know how the program's running time changes when it uses these macros.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


#include <stdio.h>

int main() {
    printf("Line: %d\n", __LINE__);
    printf("File: %s\n", __FILE__);
    return 0;
}
    

This code prints the current line number and file name using predefined macros.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Two calls to printf using predefined macros.
  • How many times: Each printf runs once, no loops or recursion.
How Execution Grows With Input

Explain the growth pattern intuitively.

Input Size (n)Approx. Operations
102
1002
10002

Pattern observation: The number of operations stays the same no matter the input size.

Final Time Complexity

Time Complexity: O(1)

This means the program runs in constant time, not changing with input size.

Common Mistake

[X] Wrong: "Using predefined macros slows down the program as input grows."

[OK] Correct: Predefined macros are replaced by the compiler before running, so they do not add extra work during execution.

Interview Connect

Understanding how predefined macros work helps you explain code behavior clearly and shows you know how compilation affects performance.

Self-Check

"What if we used a loop to print the line number multiple times? How would the time complexity change?"