Predefined macros give you useful information about your program and environment automatically. They help you write code that knows about itself.
Predefined macros
Introduction
Syntax
C
__FILE__ __LINE__ __DATE__ __TIME__ __func__
These macros are automatically defined by the compiler.
You do not need to define or include anything special to use them.
Examples
C
printf("File: %s\n", __FILE__);C
printf("Line: %d\n", __LINE__);C
printf("Compiled on %s at %s\n", __DATE__, __TIME__);C
void example() { printf("Function: %s\n", __func__); }
Sample Program
This program prints the file name, line number, compilation date and time, and the current function name using predefined macros.
C
#include <stdio.h> void show_info() { printf("File: %s\n", __FILE__); printf("Line: %d\n", __LINE__); printf("Compiled on %s at %s\n", __DATE__, __TIME__); printf("Function: %s\n", __func__); } int main() { show_info(); return 0; }
Important Notes
The exact output for __DATE__ and __TIME__ depends on when you compile the program.
__LINE__ changes depending on where you use it in the code.
__func__ is available in C99 and later standards.
Summary
Predefined macros provide automatic info about your code and environment.
They are useful for debugging and logging.
You use them by simply writing their names in your code.
Practice
1. Which of the following predefined macros in C gives the current line number in the source code?
easy
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]
Hint: Line number macro always has LINE in its name [OK]
Common Mistakes:
- Confusing __FILE__ with line number
- Using __DATE__ or __TIME__ for line info
- Thinking __LINE__ is a function
2. Which of the following is the correct syntax to print the current file name using a predefined macro in C?
easy
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]
Hint: Predefined macros always have double underscores before and after [OK]
Common Mistakes:
- Omitting underscores in __FILE__
- Using FILE without underscores
- Using lowercase file instead of macro
3. What will be the output of the following C code snippet?
#include <stdio.h>
int main() {
printf("Date: %s\n", __DATE__);
printf("Time: %s\n", __TIME__);
return 0;
}medium
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]
Hint: __DATE__ and __TIME__ show compile time info as strings [OK]
Common Mistakes:
- Expecting runtime current date/time
- Thinking macros print their own names
- Assuming macros are undefined
4. Consider this C code snippet:
What is the problem with this code?
#include <stdio.h>
int main() {
printf("Line: %d\n", LINE);
return 0;
}What is the problem with this code?
medium
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]
Hint: Predefined macros always have double underscores before and after [OK]
Common Mistakes:
- Using LINE without underscores
- Thinking %d is wrong for line number
- Ignoring missing macro error
5. You want to log the current file name, line number, and compilation date in your C program for debugging. Which code snippet correctly uses predefined macros to do this?
hard
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]
Hint: Use __FILE__, __LINE__, __DATE__ directly without quotes [OK]
Common Mistakes:
- Using macros without underscores
- Putting macros inside quotes (prints names, not values)
- Using single underscores instead of double
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
