Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Preprocessor

Predefined macros

Reference for C predefined macros: __FILE__, __LINE__, __func__, __DATE__, __TIME__, __STDC__, __STDC_VERSION__ and __STDC_HOSTED__, with examples.

A handful of macros exist before your program defines anything. The C standard requires every compiler to provide them, so you can rely on them for error messages, logging, and checking which language version you are being compiled with. Their names begin with two underscores (or an underscore and a capital letter), which reserves them for the implementation: you must never #define or #undef them.

Some of these names are true macros that the preprocessor replaces with text, while __func__ is a predefined identifier that the compiler provides inside every function body. Compilers add many more of their own (such as __GNUC__), but only the ones below are portable.

C
printf("%s:%d\n", __FILE__, __LINE__);

Try it

C Where am I?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Checking the language version

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The standard macros

Name Expands to
__FILE__ A string literal with the name of the current source file (the name as the compiler was given it, or as changed by #line).
__LINE__ An integer constant: the current line number in the source file.
__DATE__ A string literal such as "Mar 5 2026": the date of translation.
__TIME__ A string literal such as "14:03:59": the time of translation.
__STDC__ 1 when the compiler conforms to the standard.
__STDC_VERSION__ A long constant: 199901L (C99), 201112L (C11), 201710L (C17), 202311L (C23). Absent in C89.
__STDC_HOSTED__ 1 for a hosted implementation (a full library and operating system), 0 for freestanding code such as firmware.
__func__ Not a macro: an implicit static const char __func__[] holding the current function's name (C99).

Optional macros exist too, for example __STDC_IEC_559__ (IEEE 754 floating point) and __STDC_NO_THREADS__ and __STDC_NO_ATOMICS__ (the implementation lacks those features). Use #ifdef to test for them.

Usage notes

  • Where they are evaluated. __FILE__ and __LINE__ describe the place where the macro is used, so a logging macro that contains them reports the caller, while a helper function that contains them reports itself. Wrap them in your own macro (#define LOG(msg) log_at(__FILE__, __LINE__, msg)) to capture the caller's location.
  • Line numbers are changed by #line. After #line 100, the next line reports 100. See #line.
  • __LINE__ inside a multi-line macro call. Which line is reported when a macro invocation spans several lines is implementation-defined. Keep invocations that use it on one line.
  • __DATE__ and __TIME__ make builds non-reproducible. Two builds of the same source produce different binaries. Many projects avoid them or override them with a fixed build date.
  • The file name may include a path. __FILE__ shows whatever path the compiler was given, which can leak your directory layout into released binaries. GCC and Clang offer -ffile-prefix-map to rewrite it.
  • __STDC_VERSION__ for feature checks. Compare with >= against the constants above rather than testing for equality; a newer standard is a superset for most purposes. Because it is a long, cast it when printing with %ld.
  • __func__ is not a string literal. You cannot concatenate it with other literals ("in " __func__ is an error). Use a format string such as "%s: ..." instead.
  • Do not redefine them. Defining or undefining any predefined name is undefined behavior.
  • Compiler-specific macros (__GNUC__, _MSC_VER, __linux__, _WIN32) are not part of the standard; test for them with #ifdef and keep that code isolated.