Predefined macros - Time & Space 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.
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 the loops, recursion, array traversals that repeat.
- Primary operation: Two calls to
printfusing predefined macros. - How many times: Each
printfruns once, no loops or recursion.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 |
| 100 | 2 |
| 1000 | 2 |
Pattern observation: The number of operations stays the same no matter the input size.
Time Complexity: O(1)
This means the program runs in constant time, not changing with input size.
[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.
Understanding how predefined macros work helps you explain code behavior clearly and shows you know how compilation affects performance.
"What if we used a loop to print the line number multiple times? How would the time complexity change?"