考虑这个示例程序:printf(“%s”),printf(“%ls”),wprintf(“%s”)和wprintf(“%ls”)有什么区别?
#include <cstdio>
#include <cwchar>
#include <string>
int main()
{
std::string narrowstr = "narrow";
std::wstring widestr = L"wide";
printf("1 %s \n", narrowstr.c_str());
printf("2 %ls \n", widestr.c_str());
wprintf(L"3 %s \n", narrowstr.c_str());
wprintf(L"4 %ls \n", widestr.c_str());
return 0;
}
的这个输出是:
1 narrow
2 wide
我想知道:
- 为什么3 & 4没有打印
- 什么差异在1 & 3和2 & 4.
- 它是否有任何区别,如果narrowstr在utf8中而且vasttr在utf16中?
你看了文档?值得注意的是[cstdio](http://www.cplusplus.com/reference/cstdio/)? – 2014-11-08 11:24:57
“vasttr is in utf16”意味着您使用的是Windows(更适合Unicode的系统使用UTF-32作为宽字符串的默认值)。如果你想在WIndows系统上使用标准的C++或C来完成除ASCII以外的任何事情,那么有很多神秘的箍环可以跳转。你也可以投入并使用WinAPI。 – Cubbi 2014-11-13 02:59:23
不要投放到MICROSOFT STUPIDITY。节省自己的痛苦并编写自己的字符串库。对于上帝的爱不要使用Windows宏观转换和其他疯狂,相信我,这是可怕的,并在混乱中各种错误蔓延。 – Owl 2017-05-12 14:38:00