char vs wchar_t

后端 未结 3 2053
醉话见心
醉话见心 2020-12-31 19:56

I\'m trying to print out a wchar_t* string. Code goes below:

#include 
#include 
#include 

char *ascii_ = \"中日         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-31 20:51

    Line 1 is not ascii, it's whatever multibyte encoding is used by your compiler at compile-time. On modern systems that's probably UTF-8. printf does not know the encoding. It's just sending bytes to stdout, and as long as the encodings match, everything is fine.

    One problem you should be aware of is that lines 3 and 4 together invoke undefined behavior. You cannot mix character-based and wide-character io on the same FILE (stdout). After the first operation, the FILE has an "orientation" (either byte or wide), and after that any attempt to perform operations of the opposite orientation results in UB.

提交回复
热议问题