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?"
Practice
Solution
Step 1: Understand predefined macros purpose
Predefined macros provide info about the code or environment automatically.Step 2: Identify macro for line number
__LINE__gives the current line number in the source file.Final Answer:
__LINE__ -> Option AQuick Check:
Line number macro = __LINE__ [OK]
- Confusing __FILE__ with line number
- Using __DATE__ or __TIME__ for line info
- Thinking __LINE__ is a function
Solution
Step 1: Recall predefined macro for file name
The macro__FILE__holds the current source file name as a string.Step 2: Check correct syntax usage
Usingprintf("File: %s", __FILE__);correctly prints the file name.Final Answer:
printf("File: %s", __FILE__); -> Option BQuick Check:
File name macro syntax = __FILE__ [OK]
- Omitting underscores in __FILE__
- Using FILE without underscores
- Using lowercase file instead of macro
#include <stdio.h>
int main() {
printf("Date: %s\n", __DATE__);
printf("Time: %s\n", __TIME__);
return 0;
}Solution
Step 1: Understand __DATE__ and __TIME__ macros
These macros expand to strings representing the date and time when the code was compiled.Step 2: Predict output of printf statements
The program prints the compilation date and time as strings, not the macro names or errors.Final Answer:
Date: current date at compilation\nTime: current time at compilation -> Option CQuick Check:
__DATE__ and __TIME__ show compile date/time [OK]
- Expecting runtime current date/time
- Thinking macros print their own names
- Assuming macros are undefined
#include <stdio.h>
int main() {
printf("Line: %d\n", LINE);
return 0;
}What is the problem with this code?
Solution
Step 1: Identify the macro usage error
The code usesLINEinstead of the correct predefined macro__LINE__.Step 2: Understand consequences
SinceLINEis undefined, this causes a compilation error.Final Answer:
LINE is not a predefined macro; it should be __LINE__ -> Option AQuick Check:
Predefined macros need double underscores [OK]
- Using LINE without underscores
- Thinking %d is wrong for line number
- Ignoring missing macro error
Solution
Step 1: Identify correct predefined macros
The macros__FILE__,__LINE__, and__DATE__provide file name, line number, and compile date respectively.Step 2: Check correct usage in printf
Using them directly without quotes and with correct underscores prints the desired info.Final Answer:
printf("File: %s, Line: %d, Date: %s\n", __FILE__, __LINE__, __DATE__); -> Option DQuick Check:
Use double underscore macros directly in printf [OK]
- Using macros without underscores
- Putting macros inside quotes (prints names, not values)
- Using single underscores instead of double
Ready to prove it?
Pass the test and this pattern moves unknown
