title: wchar_t* with UTF8 chars in MSVC
body: <p>I am trying to format <code>wchar_t*</code> with UTF-8 characters using <code>vsnprintf</code> and then printing the buffer using <code>printf</code>.</p>
  
  <p>Given the following code:</p>
  
  <pre><code>/*
    This code is modified version of KB sample:
    https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/rtref/vsnprintf.htm
  
    The usage of `setlocale` is required by my real-world scenario,
    but can be modified if that fixes the issue.
  */
  
  #include &lt;wchar.h&gt;
  #include &lt;stdarg.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;locale.h&gt;
  
  #ifdef MSVC
  #include &lt;windows.h&gt;
  #endif
  
  void vout(char *string, char *fmt, ...)
  {
     setlocale(LC_CTYPE, "en_US.UTF-8");
     va_list arg_ptr;
  
     va_start(arg_ptr, fmt);
     vsnprintf(string, 100, fmt, arg_ptr);
     va_end(arg_ptr);
  }
  
  int main(void)
  {
     setlocale(LC_ALL, "");
  #ifdef MSVC
     SetConsoleOutputCP(65001); // with or without; no dice
  #endif
  
     char string[100];
  
     wchar_t arr[] = { 0x0119 };
     vout(string, "%ls", arr);
     printf("This string should have 'ę' (e with ogonek / tail) after colon:  %s\n", string);
     return 0;
  }
  </code></pre>
  
  <p>I compiled with gcc v5.4 on Ubuntu 16 to get the desired output in BASH:</p>
  
  <pre><code>gcc test.c -o test_vsn
  ./test_vsn
  This string should have 'ę' (e with ogonek / tail) after colon:  ę
  </code></pre>
  
  <p>However, on Windows 10 with CL v19.10.25019 (VS 2017), I get weird output in CMD:</p>
  
  <pre><code>cl test.c /Fetest_vsn /utf-8
  .\test_vsn
  This string should have 'T' (e with ogonek / tail) after colon:  e
  </code></pre>
  
  <p>(the <code>ę</code> before colon becomes <code>T</code> and after the colon is <code>e</code> without ogonek)</p>
  
  <p>Note that I used CL's new <code>/utf-8</code> switch (introduced in VS 2015), which apparently has no effect with or without. Based on their <a href="https://blogs.msdn.microsoft.com/vcblog/2016/02/22/new-options-for-managing-character-sets-in-the-microsoft-cc-compiler/" rel="nofollow noreferrer">blog post</a>:</p>
  
  <blockquote>
    <p>There is also a /utf-8 option that is a synonym for setting “/source-charset:utf-8” and “/execution-charset:utf-8”.</p>
  </blockquote>
  
  <p>(my source file already has BOM / utf8'ness and execution-charset is apparently not helping)</p>
  
  <p>What could be the minimal amount of changes to the code / compiler switches to make the output look identical to that of gcc?</p>