本文介绍了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之间有什么区别.
- 如果utf8中的指标为窄而utf16中的指标为宽,这有什么区别吗?
推荐答案
您需要这样做:
wprintf(L"3 %hs \n", narrowstr.c_str());
wprintf(L"4 %s \n", widestr.c_str());
为什么?因为对于printf
,%s 说的是窄字符字符串.对于wprintf
,%ls 表示宽.
Why? Because for printf
, %s says narrow-char-string. For wprintf
, %ls says wide.
但是,对于wprintf
,%s 表示宽,%ls 表示宽. %hs 表示狭窄(两者均适用).对于printf
,%s ,以这种方式表示%hs
But, for wprintf
, %s implies wide, %ls would mean wide itself. %hs would mean narrow (for both). For printf
, %s, in this manner would simply mean %hs
在VC ++/Windows上,%S
(大写S)将使效果相反.因此,对于printf("%S")
,它的意思是宽,而wprintf("%S")
的意思是窄.这对_tprintf
很有用.
On VC++/Windows, %S
(capital S), would reverse the effect. Therfore for printf("%S")
it would mean wide, and wprintf("%S")
would mean narrow. This is useful for _tprintf
.
这篇关于printf(“%s"),printf(“%ls"),wprintf(“%s")和wprintf(“%ls")之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!