Discover how your code can tell you exactly where and when it runs without you lifting a finger!
Why Predefined macros? - Purpose & Use Cases
Imagine writing a C program where you need to include the current date, time, or file name manually every time you compile or debug. You have to type these details yourself or update them each time you change the code.
This manual method is slow and error-prone. You might forget to update the date or file name, leading to confusion during debugging. It also wastes time and makes your code less reliable.
Predefined macros automatically provide useful information like the current file name, line number, compilation date, and time. This saves you from typing or updating these details manually, making your code smarter and easier to maintain.
#include <stdio.h> int main() { printf("File: myfile.c\n"); printf("Line: 10\n"); return 0; }
#include <stdio.h> int main() { printf("File: %s\n", __FILE__); printf("Line: %d\n", __LINE__); return 0; }
It enables your program to automatically know and display important information about itself without extra effort.
When debugging a program, predefined macros like __FILE__ and __LINE__ help you quickly find where an error happened without manually adding that info.
Predefined macros save time by automating code information.
They reduce errors caused by manual updates.
They make debugging and logging easier and more reliable.
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
