从http://www.globalyzer.com/gzserver/help/localeSensitiveMethods/formatting.htm#larges
不合格字符串说明符(大%S)
这些表显示了Windows和ANSI如何根据%S说明符和函数调用样式(单个基因RIC,或宽):
Windows function Specifier Parameter needs to be
printf/sprintf (single/MBCS) %S wchar_t*
_tprintf/_stprintf (generic) %S (don't use)
wprintf/swprintf (wide) %S char*
ANSI function Specifier Parameter needs to be
printf/sprintf (single/MBCS) %S wchar_t*
wprintf/swprintf (wide) %S wchar_t*
ANSI和视窗治疗%S基本上在单个字节或宽,这讽刺意味着Windows和ANSI再次不同的方式处理这些说明符的条款相对的%s。
请注意,ANSI本质上总是以与%ls相同的方式处理%S,换句话说它总是假定为宽字符串。
另一方面,Windows根据函数调用的类型对待%S。对于单字节函数调用,%S的作用类似于宽的%ls说明符,但对于宽泛函数调用,%S的作用与单字节%hs说明符相同。
此说明符不应用于Windows通用调用。由于%S是%s的“反向”,因此如果_UNICODE标志关闭,参数将需要较宽,如果_UNICODE标志打开,则需要单个字节。 TCHAR泛型类型不能以这种方式工作,并且没有“反TCHAR”类型的数据类型。
我尝试以下在Visual C++ 2010:
#include "stdafx.h"
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
WCHAR cBuf[MAX_PATH];
TCHAR tBuf[MAX_PATH];
wcsncpy_s(cBuf, L"Testing\r\n", MAX_PATH);
_tcsncpy_s(tBuf, _T("Testing\r\n"), MAX_PATH);
printf("%S", cBuf); // Microsoft extension
printf("%ls", cBuf); // works in VC++ 2010
wprintf(L"%s", cBuf); // wide
_tprintf(_T("%s"), tBuf); // single-byte/wide
return 0;
}
设置:
/ZI/NOLOGO/W3/WX-/ OD/Oy-/d “WIN32”/ d “_DEBUG”/D“_CONSOLE”/ D“_UNICODE”/ D“UNICODE”/ Gm/EHsc/RTC1/GS/fp:precise/Zc:wchar_t/Zc:forScope /Yu"StdAfx.h“/Fp"Debug\TestWchar.pch “/ Fa”Debug \“/ Fo”Debug \“/Fd"Debug\vc100.pdb”/ Gd/analyze-/errorReport:队列
您使用的编译器是什么?有什么选择? – geoffspear
'%S'应该做什么? –
@Kerrek SB:打印宽字符 – Joe