Friday, August 7, 2009

Microsoft Pre-defined macros

_MSC_VER

This is the predefined macro, which identifies the Micro

Soft Compiler VERsion. It comprises of the major and minor number components of the compiler’s version number.

Open your Visual studio Command Prompt and type in “cl/” and hit enter. The output in my system is shown below

.

So, the VC++ compiler version of my system is 15.00.21022.08. In this period delimited version number, the major number corresponds to the first component , 15 and the minor number corresponds to the second component, 00. So for Visual studio 2008, the _MSC_VER macro evaluates to 1500.

Below are a couple of examples of the usage of _MSC_VER macro in programming.

Example1:

#ifdef _MSC_VER

//if it is any version of MicroSoft Compiler, the below main() function

// is executed. Else this portion of code will be inactive.

int main(int argc, char* argv[])

{

//some come

return 0;

}

#else

//if it is any other compiler other than MicroSoft, the below main()

//function is executed. Else this portion of code will be inactive.

int main(int argc, char* argv[])

{

//some come

return 0;

}

#endif

In the above code snippet, it is shown that it has two ‘main()’ functions. But out of these two, at any point, only one will be active and the other is totally inactive. The compiler only identifies the main() function relevant to it. There by this does not result in any compiler error.

Example2:

#if (_MSC_VER >= 1400)

//VC 8.0: Executes this part of the code if the Compiler is VC 8.0

// and above.

#elseif (_MSC_VER >= 1310)

// VC 7.1: Executes this part of the code if the Compiler is VC 7.1.

#elseif (_MSC_VER >= 1300)

// VC 7.0: Executes this part of the code if the Compiler is VC 7.0.

#else

// VC 6.0: Executes this part of the code if the Compiler is VC 6.0.

#endif

In the above example a distinction is made between the various versions of Microsoft compiler. So based on the compiler version that part of the code executes and not all.

No comments: