Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Preprocessor, Tools and Concepts

Predefined macros

Reference for C++ predefined macros: __cplusplus, __FILE__, __LINE__, __DATE__, __TIME__, __STDC_HOSTED__, __func__, __COUNTER__ and compiler macros.

Every conforming C++ compiler defines a handful of macros before it reads your first line. They report which language standard is in effect, where the preprocessor currently is in the source (file and line), when the translation unit was compiled and whether the implementation is a full "hosted" one. On top of that, each compiler adds its own macros that describe the compiler version and the target system.

The standard macros are always available; their names start with two underscores (or an underscore and a capital letter) and must never be defined, undefined or reused for your own purposes. Compiler-specific macros such as __GNUC__, _MSC_VER and __linux__ are not part of the standard, but are the usual way to tell platforms apart.

C++
std::cout << __FILE__ << ':' << __LINE__ << '\n';   // the file and line of this statement
#if __cplusplus >= 202002L
    // C++20 or newer
#endif

Try it

C++ Language level, line numbers and __func__

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 std::cin or std::getline), it gets the next line from this box.

C++ __COUNTER__, __DATE__ and __TIME__

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 std::cin or std::getline), it gets the next line from this box.

Syntax

Macro Value
__cplusplus 199711L (C++98), 201103L (C++11), 201402L (C++14), 201703L (C++17), 202002L (C++20), 202302L (C++23). Compilers set it from the -std option. MSVC reports 199711L unless you pass /Zc:__cplusplus.
__FILE__ String literal: the presumed name of the current source file.
__LINE__ Integer literal: the presumed line number in the current file.
__DATE__ String literal "Mmm dd yyyy": compile date, with the day padded by a space when below 10.
__TIME__ String literal "hh:mm:ss": compile time.
__STDC_HOSTED__ 1 for a hosted implementation (with the full standard library and an OS), 0 for freestanding.
__STDCPP_THREADS__ 1 if the implementation can run more than one thread (implementation-defined presence).
__STDCPP_DEFAULT_NEW_ALIGNMENT__ Default alignment of operator new (C++17).
__VA_ARGS__, __VA_OPT__ Only inside variadic macro replacement lists.
__func__ Not a macro: a static const char[] implicitly defined in every function body (the exact text for templates and members is implementation-defined).

The #line directive sets the presumed line number (and optionally file name) used by __LINE__, __FILE__ and by diagnostics: #line 500 or #line 20 "other.cpp".

Usage notes

  • __cplusplus selects code paths. Test __cplusplus >= 202002L for the language level, but use feature-test macros to find out whether a specific feature exists; compilers often implement a standard only partially.
  • __DATE__ and __TIME__ make builds non-reproducible, since the output changes on every compile. GCC warns with -Wdate-time. Avoid them in release code and use a build-system supplied version string.
  • __FILE__ leaks the build path into the binary. Compilers offer -ffile-prefix-map=old=new to shorten it for reproducible builds.
  • Do not define or undefine standard macros. The result is undefined behavior; only #line may alter __LINE__ and __FILE__.
  • Capture call sites with std::source_location. __LINE__ inside a function reports the line of the function body, not of the caller. A default argument std::source_location::current() gets the caller's position without a macro.
  • Compiler macros: __GNUC__, __clang__, _MSC_VER, and platform macros such as _WIN32, __linux__, __APPLE__, __x86_64__. Clang also defines __GNUC__, so test __clang__ first when the difference matters.
  • See them all: g++ -dM -E -x c++ /dev/null prints every macro predefined by GCC for the current options.
  • __COUNTER__ is not standard but is supported by GCC, Clang and MSVC; it is handy for generating unique names, and each expansion in a translation unit increases it, so do not use it in headers that may expand it a different number of times in different files if you need matching values.
  • __func__ lets logging code name the current function; __PRETTY_FUNCTION__ (GCC/Clang) includes the signature and template arguments.