How to properly add hex escapes into a string-literal?

后端 未结 3 831
轻奢々
轻奢々 2020-11-28 11:26

When you have string in C, you can add direct hex code inside.

char str[] = \"abcde\"; // \'a\', \'b\', \'c\', \'d\', \'e\', 0x00
char str2[] = \"abc\\x12\\x         


        
3条回答
  •  半阙折子戏
    2020-11-28 12:11

    Why I'm asking? When you want to build UTF-8 string as constant, you have to use hex values of character is larger than ASCII table can hold.

    Well, no. You don't have to. As of C11, you can prefix your string constant with u8, which tells the compiler that the character literal is in UTF-8.

    char solution[] = u8"no need to use hex-codes áé§µ";
    

    (Same thing is supported by C++11 as well, by the way)

提交回复
热议问题