#ifdef _WIN32

When writing processor-specific code, the _M_IX86, _M_AMD64 and _M_IA64 can be used for conditional compilation — so far, so good. But sometimes code is not exactly processor-specific but rather specific to the natural machine word length (i.e. 32 bit or 64 bit). Fur such situations, there are defines, too — however there is a little catch: For ancient 16 bit code, there is _WIN16. For 64 bit, the WDK build environment defines _WIN64 by default. Given these two macros, it is tempting to conclude that _WIN32 should only be defined for 32 bit builds — however this is not the case. As it turns out, _WIN32 is always defined, both for 32 and 64 bit builds.

And yes, this behaviour is documented on MSDN, but it is stupid anyway.

However, where _WIN32 can be of use is when writing code targeting multiple platforms — as _WIN32 is always defined, it can be used as an indicator that you compile for Windows, regardless of the compiler used (another option is to use _MSC_VER, but that is compiler-specific).


3 Responses to “#ifdef _WIN32”

  1. 1 Roman Ryltsov May 2, 2011 at 1:04 pm

    Yeah it may look sick but perhaps it has quite some reasons behind the decision. _WIN32 is introduced quite some time ago and compared to 16-bit Windows it was a completely different world. Not so much difference now between 32-bit and 64-bit code and I am pretty sure they decided to keep _WIN32 defined for 64-bit code for compatibility and code migration reasons.

  2. 2 WndSks May 4, 2011 at 10:00 am

    It is not a big problem in practice since you often end up writing something like:

    #ifdef _WIN64

    For cross platform builds, the fact that _WIN32 is defined for 32 and 64 also comes in handy:

    #ifdef _WIN32

  3. 3 Somasundaram July 8, 2011 at 10:24 pm


    Is there a way we can avoid these #ifdef to make code compatible with both 32 bit and 64 bit?


Comments are currently closed.


About me

Johannes Passing, M.Sc., living in Berlin, Germany.

Besides his consulting work, Johannes mainly focusses on Win32, COM, and NT kernel mode development, along with Java and .Net. He also is the author of cfix, a C/C++ unit testing framework for Win32 and NT kernel mode, Visual Assert, a Visual Studio Unit Testing-AddIn, and NTrace, a dynamic function boundary tracing toolkit for Windows NT/x86 kernel/user mode code.

Contact Johannes: jpassing (at) acm org

Johannes' GPG fingerprint is BBB1 1769 B82D CD07 D90A 57E8 9FE1 D441 F7A0 1BB1.

LinkedIn Profile
Xing Profile
Github Profile

%d bloggers like this: