C Language Preprocessor and Macros Predefined Macros


Example

A predefined macro is a macro that is already understood by the C pre processor without the program needing to define it. Examples include

Mandatory Pre-Defined Macros

  • __FILE__, which gives the file name of the current source file (a string literal),
  • __LINE__ for the current line number (an integer constant),
  • __DATE__ for the compilation date (a string literal),
  • __TIME__ for the compilation time (a string literal).

There's also a related predefined identifier, __func__ (ISO/IEC 9899:2011 §6.4.2.2), which is not a macro:

The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration:

 static const char __func__[] = "function-name";

appeared, where function-name is the name of the lexically-enclosing function.

__FILE__, __LINE__ and __func__ are especially useful for debugging purposes. For example:

fprintf(stderr, "%s: %s: %d: Denominator is 0", __FILE__, __func__, __LINE__);

Pre-C99 compilers, may or may not support __func__ or may have a macro that acts the same that is named differently. For example, gcc used __FUNCTION__ in C89 mode.

The below macros allow to ask for detail on the implementation:

  • __STDC_VERSION__ The version of the C Standard implemented. This is a constant integer using the format yyyymmL (the value 201112L for C11, the value 199901L for C99; it wasn't defined for C89/C90)
  • __STDC_HOSTED__ 1 if it's a hosted implementation, else 0.
  • __STDC__ If 1, the implementation conforms to the C Standard.

Other Pre-Defined Macros (non mandatory)

ISO/IEC 9899:2011 §6.10.9.2 Environment macros:

  • __STDC_ISO_10646__ An integer constant of the form yyyymmL (for example, 199712L). If this symbol is defined, then every character in the Unicode required set, when stored in an object of type wchar_t, has the same value as the short identifier of that character. The Unicode required set consists of all the characters that are defined by ISO/IEC 10646, along with all amendments and technical corrigenda, as of the specified year and month. If some other encoding is used, the macro shall not be defined and the actual encoding used is implementation-defined.

  • __STDC_MB_MIGHT_NEQ_WC__ The integer constant 1, intended to indicate that, in the encoding for wchar_t, a member of the basic character set need not have a code value equal to its value when used as the lone character in an integer character constant.

  • __STDC_UTF_16__ The integer constant 1, intended to indicate that values of type char16_t are UTF−16 encoded. If some other encoding is used, the macro shall not be defined and the actual encoding used is implementation-defined.

  • __STDC_UTF_32__ The integer constant 1, intended to indicate that values of type char32_t are UTF−32 encoded. If some other encoding is used, the macro shall not be defined and the actual encoding used is implementation-defined.

ISO/IEC 9899:2011 §6.10.8.3 Conditional feature macros

  • __STDC_ANALYZABLE__ The integer constant 1, intended to indicate conformance to the specifications in annex L (Analyzability).
  • __STDC_IEC_559__ The integer constant 1, intended to indicate conformance to the specifications in annex F (IEC 60559 floating-point arithmetic).
  • __STDC_IEC_559_COMPLEX__ The integer constant 1, intended to indicate adherence to the specifications in annex G (IEC 60559 compatible complex arithmetic).
  • __STDC_LIB_EXT1__ The integer constant 201112L, intended to indicate support for the extensions defined in annex K (Bounds-checking interfaces).
  • __STDC_NO_ATOMICS__ The integer constant 1, intended to indicate that the implementation does not support atomic types (including the _Atomic type qualifier) and the <stdatomic.h> header.
  • __STDC_NO_COMPLEX__ The integer constant 1, intended to indicate that the implementation does not support complex types or the <complex.h> header.
  • __STDC_NO_THREADS__ The integer constant 1, intended to indicate that the implementation does not support the <threads.h> header.
  • __STDC_NO_VLA__ The integer constant 1, intended to indicate that the implementation does not support variable length arrays or variably modified types.