Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://blogs.msdn.com/b/vcblog/archive/2014/06/18/crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1.aspx" rel="nofollow">Visual Studio 14 CTP1</a> and later will always treat <code>%s</code> as a narrow string (<code>char*</code>) unless you define <code>_CRT_STDIO_LEGACY_WIDE_SPECIFIERS</code>. It also added the T length modifier extension which maps to what MS calls the "natural" width. For <code>sprintf</code> <code>%Ts</code> is <code>char*</code> and for <code>swprintf</code> <code>%Ts</code> is <code>wchar_t*</code>.</p> <hr> <p>In Visual Studio 13 and earlier <code>%s</code>/<code>%c</code> is mapped to the natural width of the function/format string and <code>%S</code>/<code>%C</code> is mapped to the opposite of the natural with:</p> <pre><code>printf("%c %C %s %S\n", 'a', L'B', "cd", L"EF"); wprintf(L"%c %C %s %S\n", L'a', 'B', L"cd", "EF"); </code></pre> <p>You can also force a specific width by using a length modifier: <code>%ls</code>, <code>%lc</code>, <code>%ws</code> and <code>%wc</code> always mean <code>wchar_t</code> and <code>%hs</code> and <code>%hc</code> are always <code>char</code>. (Documented for VS2003 <a href="http://msdn.microsoft.com/en-us/library/tcxf1dw6%28v=vs.71%29.aspx" rel="nofollow">here</a> and VC6 <a href="http://msdn.microsoft.com/en-us/library/aa272936%28v=vs.60%29.aspx" rel="nofollow">here</a> (Not sure about <code>%ws</code> and when it was really added)) </p> <p>Mapping <code>%s</code> to the natural width of the function was really handy back in the days of Win9x vs. WinNT, by using the <a href="http://msdn.microsoft.com/en-us/library/c426s321.aspx" rel="nofollow"><code>tchar.h</code> header</a> you could build narrow and wide releases from the same source. When <code>_UNICODE</code> is defined the functions in <code>tchar.h</code> map to the wide functions and <code>TCHAR</code> is <code>wchar_t</code>, otherwise the narrow functions are used and <code>TCHAR</code> is <code>char</code>:</p> <pre><code>_tprintf(_T("%c %s\n"), _T('a'), _T("Bcd")); </code></pre> <p>There is a similar convention used by the Windows SDK header files and the few format functions that exist there (wsprintf, wvsprintf, wnsprintf and wvnsprintf) but they are controlled by <code>UNICODE</code> and <code>TEXT</code> and not <code>_UNICODE</code> and <code>_T</code>/<code>_TEXT</code>.</p> <p>You probably have 3 choices to make a multi-platform project work on Windows if you want to support older Windows compilers:</p> <p><strong>1)</strong> Compile as a narrow string project on Windows, probably not a good idea and in your case swprintf will still treat %s as wchar_t*.</p> <p><strong>2)</strong> Use custom defines similar to how inttypes.h format strings work:</p> <pre><code>#ifdef _WIN32 #define PRIs "s" #define WPRIs L"hs" #else #define PRIs "s" #define WPRIs L"s" #endif printf("%" PRIs " World\n", "Hello"); wprintf(L"%" WPRIs L" World\n", "Hello"); </code></pre> <p><strong>3)</strong> Create your own custom version of swprintf and use it with Visual Studio 13 and earlier.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload