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

C Predefined Macros

Predefined macros are ready-made names C fills in for you, like the current file, line, date, and time. They help with messages and debugging.
Syntax
c
__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

c
// 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;
}

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 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;
}

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 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;
}

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 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;
}

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 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming __DATE__ and __TIME__ show when the program runs, when they show the compile time.
  2. Printing __LINE__ with %s, when it is an integer and needs %d.
  3. Leaving out the double underscores on either side of the name, as in FILE.
Chapter Summary
  • The preprocessor runs before compilation and handles lines that start with #.
  • #define creates constants and macros, and #include brings 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:

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.