title: Difference in preprocessor output between Mingw gcc and cl.exe
body: <p>In some Windows C/C++ code I am using the following slightly modified piece of code from <a href="https://stackoverflow.com/a/49349920">here</a> to either print to the stdout or to the debug output depending on the type of compilation, specified by <code>#define DEBUG</code>.</p>
  <p>The print to the debug output works as expected, and also automatically appends a newline. For that reason I append a <code>\n</code> in the stdout case. It turns out however that there are different outputs when compiling with Visual Studio's <code>cl.exe</code> compiler and with the Mingw gcc compiler (<code>x86_64-w64-mingw32-gcc</code>).</p>
  <p><strong>Code</strong></p>
  <pre><code>void printf(char* format, ...);
  
  #define _SELECT(PREFIX,_5,_4,_3,_2,_1,SUFFIX,...) PREFIX ## _ ## SUFFIX
  #define PRINTF(...) _SELECT(_PRINTF,__VA_ARGS__,N,N,N,N,1)(__VA_ARGS__)
  #ifdef DEBUG
  #define _PRINTF_1(fmt)      debugprint(1, fmt)
  #define _PRINTF_N(fmt, ...) debugprint(1, fmt, __VA_ARGS__);
  #else
  #define _PRINTF_1(fmt)      printf(fmt &quot;\n&quot;)
  #define _PRINTF_N(fmt, ...) printf(fmt &quot;\n&quot;, __VA_ARGS__);
  #endif
  
  int main(int argc, char* argv[])
  {
      PRINTF(&quot;Hello World!&quot;);
      PRINTF(&quot;Hello %s!&quot;, &quot;World&quot;);
  }
  </code></pre>
  <p>When compiling this code with the Mingw gcc compiler (<code>x86_64-w64-mingw32-gcc helloworld.cpp -o helloworld.exe</code> - <a href="https://godbolt.org/z/9WPfh5GW1" rel="nofollow noreferrer">Compiler Explorer</a>), the binary shows the following output.</p>
  <pre><code>Hello World!
  Hello World!
  </code></pre>
  <p>After compiling with Visual Studio cl.exe (<code>cl.exe helloworld.cpp /Fehelloworld.exe</code> - <a href="https://godbolt.org/z/jEa8sq8n4" rel="nofollow noreferrer">Compiler Explorer</a>) though, executing the binary shows the following output.</p>
  <pre><code>Hello World!
  Hello World
  !
  </code></pre>
  <p>This means that for some reason the <code>cl.exe</code> compiler appends the <code>\n</code> to the <code>&quot;World&quot;</code> argument as opposed to appending it at the end of the format string.</p>
  <p>Any suggestions on how to improve this preprocessor code so it yields the same results in both Mingw gcc and cl.exe?</p>