Predefined macros give you useful information about your program and environment automatically. They help you write code that knows about itself.
0
0
Predefined macros
Introduction
To print the current file name and line number for debugging.
To check the date and time when the program was compiled.
To get the name of the current function for logging.
To write code that behaves differently depending on the compiler or system.
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
Prints the name of the current source file.
C
printf("File: %s\n", __FILE__);Prints the current line number in the source file.
C
printf("Line: %d\n", __LINE__);Prints the date and time when the program was compiled.
C
printf("Compiled on %s at %s\n", __DATE__, __TIME__);Prints the name of the current function.
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; }
OutputSuccess
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.