Condividi tramite


Predefined Macros

 

The latest version of this topic can be found at Predefined Macros.

The Visual C++ compiler predefines certain preprocessor macros, depending on the language (C or C++), the compilation target, and the chosen compiler options.

Visual C++ supports the required predefined preprocessor macros specified by the ANSI/ISO C99 standard and the ISO C++14 standard. The implementation also supports several more Microsoft-specific preprocessor macros. Some macros are defined only for specific build environments or compiler options. Unless noted, the macros are defined throughout a translation unit as if they were specified as /D compiler option arguments. When defined, the macros are expanded to the specified values by the preprocessor before compilation. The predefined macros take no arguments and cannot be redefined.

Standard predefined identifier

The compiler supports this predefined identifier specified by ISO C99 and ISO C++11.

  • __func__ The unqualified and unadorned name of the enclosing function as a function-local static const array of char.

    void example(){  
        printf("%s\n", __func__);  
    } // prints "example"  
    

Standard predefined macros

The compiler supports these predefined macros specified by the ISO C99 and ISO C++14 standards.

  • __cplusplus Defined as an integer literal value when the translation unit is compiled as C++. Otherwise, undefined.

  • __DATE__ The compilation date of the current source file. The date is a constant length string literal of the form Mmm dd yyyy. The month name Mmm is the same as the abbreviated month name in dates generated by the C Runtime Library asctime function. The first character of date dd is a space if the value is less than 10. This macro is always defined.

  • __FILE__ The name of the current source file. __FILE__ expands to a character string literal. To ensure that the full path to the file is displayed, use /FC (Full Path of Source Code File in Diagnostics). This macro is always defined.

  • __LINE__ Defined as the integer line number in the current source file. The value of the __LINE__ macro can be changed by using a #line directive. This macro is always defined.

  • __STDC__ Defined as 1 only when compiled as C and if the /Za compiler option is specified. Otherwise, undefined.

  • __STDC_HOSTED__ Defined as 1 if the implementation is a hosted implementation, one that supports the entire required standard library. Otherwise, defined as 0.

  • __STDCPP_THREADS__ Defined as 1 if and only if a program can have more than one thread of execution, and compiled as C++. Otherwise, undefined.

  • __TIME__ The time of translation of the preprocessed translation unit. The time is a character string literal of the form hh:mm:ss, the same as the time returned by the C Runtime Library asctime function. This macro is always defined.

Microsoft-specific predefined macros

Microsoft Visual C++ supports these additional predefined macros.

  • __ATOM__ Defined as 1 when the /favor:ATOM compiler option is set and the compiler target is x86 or x64. Otherwise, undefined.

  • __AVX__ Defined as 1 when the /arch:AVX or /arch:AVX2 compiler options are set and the compiler target is x86 or x64. Otherwise, undefined.

  • __AVX2__ Defined as 1 when the /arch:AVX2 compiler option is set and the compiler target is x86 or x64. Otherwise, undefined.

  • _CHAR_UNSIGNED Defined as 1 if the default char type is unsigned. This is set when the /J (Default char Type Is unsigned) compiler option is set. Otherwise, undefined.

  • __CLR_VER Defined as an integer literal that represents the version of the common language runtime used when the application was compiled. The value is encoded in the form Mmmbbbbb, where M is the major version of the runtime, mm is the minor version of the runtime, and bbbbb is the build number. __CLR_VER is defined if the /clr compiler option is set. Otherwise, undefined.

    // clr_ver.cpp  
    // compile with: /clr  
    using namespace System;  
    int main() {  
       Console::WriteLine(__CLR_VER);  
    }  
    
  • _CONTROL_FLOW_GUARD Defined as 1 when the /guard:cf (Enable Control Flow Guard) compiler option is set. Otherwise, undefined.

  • __COUNTER__ Expands to an integer literal that starts at 0 and is incremented by 1 every time it is used in a source file or included headers of the source file. __COUNTER__ remembers its state when you use precompiled headers. This macro is always defined.

    This example uses __COUNTER__ to assign unique identifiers to three different objects of the same type. The exampleClass constructor takes an integer as a parameter. In main, the application declares three objects of type exampleClass, using __COUNTER__ as the unique identifier parameter:

    // macro__COUNTER__.cpp  
    // Demonstration of __COUNTER__, assigns unique identifiers to  
    // different objects of the same type.  
    // Compile by using: cl /EHsc /W4 macro__COUNTER__.cpp  
    #include <stdio.h>  
    
    class exampleClass {  
        int m_nID;  
    public:  
        // initialize object with a read-only unique ID  
        exampleClass(int nID) : m_nID(nID) {}  
        int GetID(void) { return m_nID; }  
    };  
    
    int main()  
    {  
        // __COUNTER__ is initially defined as 0  
        exampleClass e1(__COUNTER__);  
    
        // On the second reference, __COUNTER__ is now defined as 1  
        exampleClass e2(__COUNTER__);  
    
        // __COUNTER__ is now defined as 2  
        exampleClass e3(__COUNTER__);  
    
        printf("e1 ID: %i\n", e1.GetID());  
        printf("e2 ID: %i\n", e2.GetID());  
        printf("e3 ID: %i\n", e3.GetID());  
    
        // Output  
        // ------------------------------  
        // e1 ID: 0  
        // e2 ID: 1  
        // e3 ID: 2  
    
        return 0;  
    }  
    
  • __cplusplus_cli Defined as the integer literal value 200406 when compiled as C++ and the /clr, /clr:pure, or /clr:safe compiler option is set. Otherwise, undefined. When defined, __cplusplus_cli is in effect throughout the translation unit.

    // cplusplus_cli.cpp  
    // compile by using /clr  
    #include "stdio.h"  
    int main() {  
       #ifdef __cplusplus_cli  
          printf("%d\n", __cplusplus_cli);  
       #else  
          printf("not defined\n");  
       #endif  
    }  
    
  • __cplusplus_winrt Defined as the integer literal value 201009 when compiled as C++ and the /ZW (Windows Runtime Compilation) compiler option is set. Otherwise, undefined.

  • _CPPRTTI Defined as 1 if the /GR (Enable Run-Time Type Information) compiler option is set. Otherwise, undefined.

  • _CPPUNWIND Defined as 1 if one or more of the /GX (Enable Exception Handling), /clr (Common Language Runtime Compilation), or /EH (Exception Handling Model) compiler options are set. Otherwise, undefined.

  • _DEBUG Defined as 1 when the /LDd, /MDd, or /MTd compiler option is set. Otherwise, undefined.

  • _DLL Defined as 1 when the /MD or /MDd (Multithreaded DLL) compiler option is set. Otherwise, undefined.

  • __FUNCDNAME__ Defined as a string literal that contains the decorated name of the enclosing function. The macro is defined only within a function. The __FUNCDNAME__ macro is not expanded if you use the /EP or /P compiler option.

    This example uses the __FUNCDNAME__, __FUNCSIG__, and __FUNCTION__ macros to display function information.

    // Demonstrates functionality of __FUNCTION__, __FUNCDNAME__, and __FUNCSIG__ macros
    void exampleFunction()
    {
        printf("Function name: %s\n", __FUNCTION__);
        printf("Decorated function name: %s\n", __FUNCDNAME__);
        printf("Function signature: %s\n", __FUNCSIG__);
    
        // Sample Output
        // -------------------------------------------------
        // Function name: exampleFunction
        // Decorated function name: ?exampleFunction@@YAXXZ
        // Function signature: void __cdecl exampleFunction(void)
    }
    
  • __FUNCSIG__ Defined as a string literal that contains the signature of the enclosing function. The macro is defined only within a function. The __FUNCSIG__ macro is not expanded if you use the /EP or /P compiler option. When compiled for a 64-bit target, the calling convention is __cdecl by default. For an example of usage, see the __FUNCDNAME__ macro.

  • __FUNCTION__ Defined as a string literal that contains the undecorated name of the enclosing function. The macro is defined only within a function. The __FUNCTION__ macro is not expanded if you use the /EP or /P compiler option. For an example of usage, see the __FUNCDNAME__ macro.

  • _INTEGRAL_MAX_BITS Defined as the integer literal value 64, the maximum size (in bits) for a non-vector integral type. This macro is always defined.

    // integral_max_bits.cpp  
    #include <stdio.h>  
    int main() {  
       printf("%d\n", _INTEGRAL_MAX_BITS);  
    }  
    
  • __INTELLISENSE__ Defined as 1 during an IntelliSense compiler pass in the Visual Studio IDE. Otherwise, undefined. You can use this macro to guard code the IntelliSense compiler does not understand, or use it to toggle between the build and IntelliSense compiler. For more information, see Troubleshooting Tips for IntelliSense Slowness.

  • _ISO_VOLATILE Defined as 1 if the /volatile:iso compiler option is set. Otherwise, undefined.

  • _KERNEL_MODE Defined as 1 if the /kernel (Create Kernel Mode Binary) compiler option is set. Otherwise, undefined.

  • _M_AMD64 Defined as the integer literal value 100 for compilations that target x64 processors. Otherwise, undefined.

  • _M_ARM Defined as the integer literal value 7 for compilations that target ARM processors. Otherwise, undefined.

  • _M_ARM_ARMV7VE Defined as 1 when the /arch:ARMv7VE compiler option is set for compilations that target ARM processors. Otherwise, undefined.

  • _M_ARM_FP Defined as an integer literal value that indicates which /arch compiler option was set, if the compilation target is an ARM processor. Otherwise, undefined.

    • In the range 30-39 if no /arch ARM option was specified, indicating the default architecture for ARM was set (VFPv3).

    • In the range 40-49 if /arch:VFPv4 was set.

    • See /arch (ARM) for more information.

  • _M_ARM64 Defined as 1 for compilations that target 64-bit ARM processors. Otherwise, undefined.

  • _M_CEE Defined as 001 if any /clr (Common Language Runtime Compilation) compiler option is set. Otherwise, undefined.

  • _M_CEE_PURE Defined as 001 if the /clr:pure compiler option is set. Otherwise, undefined.

  • _M_CEE_SAFE Defined as 001 if the /clr:safe compiler option is set. Otherwise, undefined.

  • _M_FP_EXCEPT Defined as 1 if the /fp:except or /fp:strict compiler option is set. Otherwise, undefined.

  • _M_FP_FAST Defined as 1 if the /fp:fast compiler option is set. Otherwise, undefined.

  • _M_FP_PRECISE Defined as 1 if the /fp:precise compiler option is set. Otherwise, undefined.

  • _M_FP_STRICT Defined as 1 if the /fp:strict compiler option is set. Otherwise, undefined.

  • _M_IX86 Defined as the integer literal value 600 for compilations that target x86 processors. This macro is not defined for x64 or ARM compilation targets.

  • _M_IX86_FP Defined as an integer literal value that indicates the /arch compiler option that was set, or the default. This macro is always defined when the compilation target is an x86 processor. Otherwise, undefined. When defined, the value is:

    • 0 if the /arch:IA32 compiler option was set.

    • 1 if the /arch:SSE compiler option was set.

    • 2 if the /arch:SSE2, /arch:AVX or /arch:AVX2 compiler option was set. This value is the default if an /arch compiler option was not specified. When /arch:AVX is specified, the macro __AVX__ is also defined. When /arch:AVX2 is specified, both __AVX__ and __AVX2__ are also defined.

    • See /arch (x86) for more information.

  • _M_X64 Defined as the integer literal value 100 for compilations that target x64 processors. Otherwise, undefined.

  • _MANAGED Defined as 1 when the /clr compiler option is set. Otherwise, undefined.

  • _MSC_BUILD Defined as an integer literal that contains the revision number element of the compiler's version number. The revision number is the fourth element of the period-delimited version number. For example, if the version number of the Visual C++ compiler is 15.00.20706.01, the _MSC_BUILD macro evaluates to 1. This macro is always defined.

  • _MSC_EXTENSIONS Defined as 1 if the /Ze (Enable Language Extensions) compiler option is set, which is the default. Otherwise, undefined.

  • _MSC_FULL_VER Defined as an integer literal that encodes the major, minor, and build number elements of the compiler's version number. The major number is the first element of the period-delimited version number, the minor number is the second element, and the build number is the third element. For example, if the version number of the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706. Enter cl /? at the command line to view the compiler's version number. This macro is always defined.

  • _MSC_VER Defined as an integer literal that encodes the major and minor number elements of the compiler's version number. The major number is the first element of the period-delimited version number and the minor number is the second element. For example, if the version number of the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Enter cl /? at the command line to view the compiler's version number. This macro is always defined.

  • _MSVC_LANG Defined as an integer literal that specifies the C++ language standard targeted by the compiler. When compiled as C++, the macro is the integer literal value 201402 if the /std:c++14 compiler option is set, or by default; it is set to 201703 if the /std:c++17 compiler option is set; and it is set to a higher, unspecified value when the /std:c++latest compiler option is set. Otherwise, the macro is undefined. The _MSVC_LANG macro and /std (Specify Language Standard Version) compiler options are available beginning in Visual Studio 2015 Update 3; /std:c++17 is available beginning in Visual Studio 2017 version 15.3.

  • __MSVC_RUNTIME_CHECKS Defined as 1 when one of the /RTC compiler options is set. Otherwise, undefined.

  • _MT Defined as 1 when /MD or /MDd (Multithreaded DLL) or /MT or /MTd (Multithreaded) is specified. Otherwise, undefined.

  • _NATIVE_WCHAR_T_DEFINED Defined as 1 when the /Zc:wchar_t compiler option is set. Otherwise, undefined.

  • _OPENMP Defined as integer literal 200203, representing the date of the OpenMP specification implemented by Visual C++, if the /openmp (Enable OpenMP 2.0 Support) compiler option is set. Otherwise, undefined.

    // _OPENMP_dir.cpp  
    // compile with: /openmp   
    #include <stdio.h>   
    int main() {  
       printf("%d\n", _OPENMP);  
    }  
    
  • _PREFAST_ Defined as 1 when the /analyze compiler option is set. Otherwise, undefined.

  • __TIMESTAMP__ Defined as a string literal that contains the date and time of the last modification of the current source file, in the abbreviated, constant length form returned by the C Runtime Library asctime function, for example, Fri 19 Aug 13:32:58 2016. This macro is always defined.

  • _VC_NODEFAULTLIB Defined as 1 when the /Zl (Omit Default Library Name) compiler option is set. Otherwise, undefined.

  • _WCHAR_T_DEFINED Defined as 1 when the default /Zc:wchar_t compiler option is set. The _WCHAR_T_DEFINED macro is defined but has no value if the /Zc:wchar_t- compiler option is set, and wchar_t is defined in a system header file included in your project. Otherwise, undefined.

  • _WIN32 Defined as 1 when the compilation target is 32-bit ARM, 64-bit ARM, x86, or x64. Otherwise, undefined.

  • _WIN64 Defined as 1 when the compilation target is 64-bit ARM or x64. Otherwise, undefined.

  • _WINRT_DLL Defined as 1 when compiled as C++ and both /ZW (Windows Runtime Compilation) and /LD or /LDd compiler options are set. Otherwise, undefined.

Preprocessor macros used to determine the ATL or MFC library version are not predefined by the compiler. These macros are defined in the headers for the library, so they are undefined in preprocessor directives before the required header is included.

  • _ATL_VER Defined in <atldef.h> as an integer literal that encodes the ATL version number.

  • _MFC_VER Defined in <afxver_.h> as an integer literal that encodes the MFC version number.

See Also

Macros (C/C++)
Preprocessor Operators
Preprocessor Directives