What's the difference between printf(“%s”), printf(“%ls”), wprintf(“%s”), and wprintf(“%ls”)?

前端 未结 3 990
闹比i
闹比i 2020-12-30 03:23

Consider this sample program:

#include 
#include 
#include 

int main()
{
    std::string narrowstr = \"narrow\";
          


        
3条回答
  •  失恋的感觉
    2020-12-30 04:10

    You need to do:

    wprintf(L"3 %hs \n", narrowstr.c_str());
    wprintf(L"4 %s \n", widestr.c_str());
    

    Why? Because for printf, %s says narrow-char-string. For wprintf, %ls says wide.

    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

    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.

提交回复
热议问题