Predefined macros (original) (raw)

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

MSVC supports the predefined preprocessor macros required by the ANSI/ISO C99, C11, and C17 standards, and the ISO C++14, C++17, and C++20 standards. The implementation also supports several more Microsoft-specific preprocessor macros.

Some macros are defined only for specific build environments or compiler options. Except where noted, the macros are defined throughout a translation unit as if they were specified as /D compiler option arguments. When defined, the preprocessor expands macros their specified values before compilation. The predefined macros take no arguments and can't be redefined.

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

The compiler supports these predefined macros specified by the ISO C99, C11, C17, and ISO C++17 standards.

// clr_ver.cpp  
// compile with: /clr  
using namespace System;  
int main() {  
   Console::WriteLine(__CLR_VER);  
}  
// 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.cpp  
// compile by using /clr  
#include "stdio.h"  
int main() {  
   #ifdef __cplusplus_cli  
      printf("%d\n", __cplusplus_cli);  
   #else  
      printf("not defined\n");  
   #endif  
}  
// 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)  
}  
// integral_max_bits.cpp  
#include <stdio.h>  
int main() {  
    printf("%d\n", _INTEGRAL_MAX_BITS);  
}  
#if _MSC_VER >= 1910  
// . . .  
#elif _MSC_VER >= 1900  
// . . .  
#else  
// . . .  
#endif  

For more information about Visual Studio 2019 16.8 and 16.9, and 16.10 and 16.11, which share the same major and minor versions (and so have the same value for _MSC_VER), see Service releases starting with Visual Studio 2017.
For more information about the history of compiler versioning, and compiler version numbers and the Visual Studio versions they correspond to, see C++ compiler versioning. Also, Visual C++ Compiler Version on the Microsoft C++ team blog.

#if !defined(_MSVC_TRADITIONAL) || _MSVC_TRADITIONAL  
// Logic using the traditional preprocessor  
#else  
// Logic using cross-platform compatible preprocessor  
#endif  
// _OPENMP_dir.cpp  
// compile with: /openmp  
#include <stdio.h>  
int main() {  
    printf("%d\n", _OPENMP);  
}  

No preprocessor macros that identify the ATL or MFC library version are predefined by the compiler. ATL and MFC library headers define these version macros internally. They're undefined in preprocessor directives made before the required header is included.