← Back to C Course | Chapter 11: Preprocessor | Lesson 5 of 5

C Predefined Macros

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

c
#include <stdio.h>
int main() {
	printf("%s", __FILE__);
	return 0;
}

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

c
#include <stdio.h>
int main() {
	printf("%d", __LINE__);
	return 0;
}

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

c
#include <stdio.h>
int main() {
	printf("%s", __DATE__);
	return 0;
}

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

c
#include <stdio.h>
int main() {
	printf("%s %s", __DATE__, __TIME__);
	return 0;
}

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

c
#include <stdio.h>
int main() {
	#if __STDC__ == 1
	printf("Standards-conformant compiler");
	#endif
	return 0;
}
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.