    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: QM Bites: Understand Windows OS Identification Preprocessor Macros</title>
        <link>https://members.accu.org/index.php/journals/2223</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #132 - April 2016 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c360/">o132</a>
                    (10)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c360-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c360+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;QM Bites: Understand Windows OS Identification Preprocessor Macros</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 07 April 2016 21:31:12 +01:00 or Thu, 07 April 2016 21:31:12 +01:00</p>
<p><strong>Summary:</strong>&nbsp;Thereâ€™s confusion between user-defined and predefined Windows 32/64-bit operating-system identification macros. Matthew Wilson shines light on the issue.</p>
<p><strong>Body:</strong>&nbsp;<h2>TL;DR:</h2>

<p>Compiler defines <code>_WIN32</code> and <code>_WIN64</code>. You define <code>WIN32</code> or <code>WIN64</code>. Carefully discriminate.</p>

<h2>Bite:</h2>

<p>When compiling for Windows 32 and 64-bit architectures, there are four preprocessor object-like macro definitions for discriminating operating system that one may encounter:</p>

<ul>
	<li><code>_WIN32</code></li>
	<li><code>_WIN64</code></li>
	<li><code>WIN32</code></li>
	<li><code>WIN64</code></li>
</ul>

<p>You must take care that you understand the origins and meanings of these.</p>

<h3>_WIN32 and WIN64</h3>

<p>The symbol <code>_WIN32</code> is defined <em>by the compiler</em> to indicate that this is a (32bit) Windows compilation. Unfortunately, for historical reasons, it is also defined for 64-bit compilation.</p>

<p>The symbol <code>_WIN64</code> is defined <em>by the compiler</em> to indicate that this is a 64-bit Windows compilation.</p>

<p>Thus:</p>

<p>To identify unambiguously whether the compilation is 64-bit Windows, one tests only <code>_WIN64</code> as in:</p>

<pre class="programlisting">
  #if defined(_WIN64)
  /* Is Windows 64-bit */
  #else
  /* Is not Windows 64-bit */
  #endif</pre>

<p>To identify unambiguously whether the compilation is 32-bit Windows, one tests both <code>_WIN32</code> and <code>_WIN64</code> as in:</p>

<pre class="programlisting">
  #if defined(_WIN32) &amp;&amp; \
      !defined(_WIN64)
  /* Is Windows 32-bit */
  #else
  /* Is not Windows 32-bit */
  #endif</pre>
  
<p>To identify unambiguously whether the compilation is a form of Windows one tests both <code>_WIN32</code> and <code>_WIN64</code> as in:</p>

<pre class="programlisting">
  #if defined(_WIN64)
  /* Is Windows 64-bit */
  #elif defined(_WIN32)
  /* Is Windows 32-bit */
  #else
  /* Not Windows */
  #endif</pre>

<h3>WIN32 and WIN64</h3>

<p>The symbol <code>WIN32</code> is defined by the user to indicate whatever the user chooses it to indicate. By convention, the definition of this symbol indicates a 32-bit Windows compilation, and nothing else! Microsoft (and other) tools generate projects with this symbol defined.</p>

<p>The symbol <code>WIN64</code> is defined by the user to indicate whatever the user chooses it to indicate. By convention, the definition of this symbol indicates a 64-bit Windows compilation, and nothing else!</p>

<p>When properly defined, these symbols can be used to indicate unambiguously the 32- and 64-bit Windows compilation contexts.</p>

<h3>Caution with WIN32 / WIN64</h3>

<p>Unfortunately, when duplicating a Win32 project to x64, the Microsoft Visual Studio wizards do not translate <code>WIN32</code> to <code>WIN64</code>. You must remember to do this yourself, in order for the inferences given above to hold. Do not add a separate <code>WIN64</code> to the x64 configuration settings: replace the existing <code>WIN32</code> with <code>WIN64</code>.</p>

<h3>Why bother with WIN32 / WIN64 (and not simply rely on _WIN32 / _WIN64)?</h3>

<p>There are doubtless many reasons. The reasons I adhere strictly to this are:</p>

<ul>
	<li>it is a widely adopted and meaningful convention, so adheres to the <em>principle of least surprise</em> [<a href="#[PoLS]">PoLS</a>].</li>
	
	<li>it facilitates the ability to emulate (parts of) other operating systems (e.g. UNIX [<a href="#[UNIXem]">UNIXem</a>]) while on Windows, which can be tremendously helpful when porting code.</li>
</ul>

<h2>References</h2>

<p class="bibliomixed"><a id="[PoLS]"></a>[PoLS] <em>The Art of UNIX Programming</em>, Eric S. Raymond, AddisonWesley, 2003</p>

<p class="bibliomixed"><a id="[UNIXem]"></a>[UNIXem] UNIXem is a simple, limited UNIXAPI emulation library for Windows. See <a href="http://synesis.com.au/software/unixem.html">http://synesis.com.au/software/unixem.html</a>.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
