• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 76
  • Last Modified:

preprocessor directive in C++


I have a project in Visual Studio with the following mentioned in preprocessor directive definitions , WIN32.

However,  in the code itself, i see checks using
#if defined (__WIN32)
 //do something

Why and what is this underscore? Would it be the same had i written it as a WIN32?
  • 2
2 Solutions
No, __WIN32 is different to _WIN32 and different to WIN32.

the purpose of the

#ifdef <MACRO_NAME>

Open in new window

is to add the enclosed statements only when the <MACRO_NAME> was defined before. the definition of a macro may happen either in the preprocessor definitions of the project, or in some code - almost ever in a header file, included before the #ifdef block.

i only found WIN32 in my sources and VC includes.

in the visual studio sdk, i also found _WIN32, in some internal windows headers. generally names with an underline prefix are reserved for internal use of the compiler or sdk. they shouldn't be user defined (although this was widely ignored in my experience).

__WIN32 (with two underlines) i didn't find anywhere.

It is likely that your project is cross-platform or (from another point of view) ready for a compiler other than Visual Studio. It means that the macro wraps the things for another compiler.

For the __WIN32, see the information at  C/C++ tip: How to detect the operating system type using compiler predefined macros that explicitly explains the meaning for the mentioned compilers (slightly below the half of the page).
No comment. The questions should be closed ;)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now