C Predefined Macros
In this page:
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 <stdio.h>
int main() {
printf("%s", __FILE__);
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 <stdio.h>
int main() {
printf("%d", __LINE__);
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 <stdio.h>
int main() {
printf("%s", __DATE__);
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 <stdio.h>
int main() {
printf("%s %s", __DATE__, __TIME__);
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 <stdio.h>
int main() {
#if __STDC__ == 1
printf("Standards-conformant compiler");
#endif
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: