C Predefined Macros
In this page:
__FILE__ // current source file name
__LINE__ // current line number
__DATE__ // compile date
__TIME__ // compile time
The FILE Macro
This is invaluable for debugging and logging -- a macro like #define LOG(msg) printf("[%s] %s\n", __FILE__, msg) automatically tags every log message with the exact source file it came from, without you having to type the filename manually.
Example: The FILE Macro
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%s", __FILE__);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The LINE Macro
Combined with __FILE__, this pair is the foundation of custom assertion and error-reporting macros that tell you exactly where in the source code something went wrong, down to the specific line.
Example: The LINE Macro
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", __LINE__);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The DATE Macro
This is useful for embedding build metadata directly into compiled binaries, such as printing a program's build date in its version or about screen without needing external build scripts.
Example: The DATE Macro
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%s", __DATE__);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The TIME Macro
Paired with __DATE__, this gives you a complete build timestamp, which is commonly combined into a single version string printed when a program starts up in debug builds.
Example: The TIME Macro
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%s %s", __DATE__, __TIME__);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The STDC Macro
Checking #if __STDC__ == 1 lets code adapt its behavior or feature usage depending on whether it's being compiled by a strictly standards-conformant compiler versus one with vendor-specific extensions enabled.
Example: The STDC Macro
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
#if __STDC__ == 1
// Print formatted text to the screen
printf("Standards-conformant compiler");
#endif
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Assuming
__DATE__and__TIME__show when the program runs, when they show the compile time. - Printing
__LINE__with%s, when it is an integer and needs%d. - Leaving out the double underscores on either side of the name, as in
FILE.
- The preprocessor runs before compilation and handles lines that start with
#. #definecreates constants and macros, and#includebrings in header files.- Conditional compilation and predefined macros let you build different versions of a program from the same source.
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: