89

I know that WIN32 denotes win32 compilation but what is _WIN32 used for?

4 Answers 4

91

To elaborate (Neil Butterworth and blue.tuxedo have already given the correct answer):

  • WIN32 is defined by the SDK or the build environment, so it does not use the implementation reserved namespace
  • _WIN32 is defined by the compiler so it uses the underscore to place it in the implementation-reserved namespace

You'll find a similar set of dual defines with nearly identical names and similar uses such as _UNICODE/UNICODE, _DEBUG/DEBUG, or maybe _DLL/DLL (I think that only the UNICODE ones get much of any use in their different versions). Though sometimes in these cases (like _UNICODE), instead of the underscore version being defined by the compiler, they are used to control what the CRT headers do:

  • _UNICODE tells the CRT headers that CRT names which can be either Unicode or ANSI (such as _tcslen() should map to the wide character variant (wcslen())
  • UNICODE does something similar for the SDK (maps Win32 APIs to their "W" variants)

Essentially the versions with the underscore are controlled by or used by the compiler team, the versions without the underscore are controlled/used by teams outside of the compiler. Of course, there's probably going to be a lot overlap due to compatibility with past versions and just general mistakes by one team or the other.

I find it confusing as hell - and find that they are used nearly interchangeably in user code (usually, when you see one defined, you'll see the other defined in the same place, because if you need one you need the other). Personally, I think that you should use the versions without the underscore (unless you're writing the compiler's runtime) and make sure they both get defined (whether via hearers or compiler switches as appropriate) when you're defining one.


Note that the SDK will define _WIN32 when building for the Mac because the compiler doesn't, kind of overstepping it's bounds. I'm not sure what projects use a Win32 API an a compiler targeting the Mac - maybe some version of Office for the Mac or something.

1
  • "I'm not sure what projects use a Win32 API an a compiler targeting the Mac - maybe some version of Office" - Office shares much code between platforms, including many Windows data types as internal currency, even when not running on Windows. Feb 26, 2022 at 15:39
62

WIN32 is a name that you could use and even define in your own code and so might clash with Microsoft's usage. _WIN32 is a name that is reserved for the implementor (in this case Microsoft) because it begins with an underscore and an uppercase letter - you are not allowed to define reserved names in your own code, so there can be no clash.

2
  • When you say 'use' do you mean i could: #define WIN32 12345 or: #ifdef WIN32 Mar 19, 2009 at 13:02
  • by 'use' I mean #ifdef and #define. _WIN32 is meant to be used by any programming in the context of #ifdef. Dec 7, 2012 at 3:01
24

WIN32 is a user-defined flag which may be required by some headers. _WIN32 is automatically defined by the visual C/C++ compiler. Since it begins with an _ followed by a capital character, it is reserved by the implementation (meaning the C/C++ toolchain provider).

I prefer to use (read) _WIN32, seems safer to me.

1
  • 1
    I agree. To check if I am compiling on Windows, _WIN32 is reliable because it is always defined. WIN32 is only defined if I define it myself or include "Windows.h".
    – Fabian
    Mar 29, 2022 at 5:01
3

TL;DR: Use _WIN32, never WIN32.

_WIN32 is automatically defined by the compiler, when you're compiling for Windows (even for x64).

WIN32 is weird:

  • It's defined in <windows.h>.
  • MinGW also defines it automatically, but stops doing so if you use -std=c++?? (as opposed to -std=gnu++??, which is the default).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.