gpt4 book ai didi

c++ - 打印通用字符

转载 作者:太空狗 更新时间:2023-10-29 20:30:26 26 4
gpt4 key购买 nike

谁能解释为什么通用字 rune 字(例如“\u00b1”)被编码为 char 字符串为 UTF-8?为什么下面打印加号/减号?

#include <iostream>
#include <cstring>
int main()
{
std::cout << "\u00b1" << std::endl;
return 0;
}

这与我当前的语言环境有关吗?

最佳答案

2.13.2. [...]

5/ A universal-character-name is translated to the encoding, in the execution character set, of the character named. If there is no such encoding, the universal-character-name is translated to an implementation defined encoding. [Note: in translation phase 1, a universal-character-name is introduced whenever an actual extended character is encountered in the source text. Therefore, all extended characters are described in terms of universal-character-names. However, the actual compiler implementation may use its own native character set, so long as the same results are obtained. ]

2.2. [...] The values of the members of the execution character sets are implementation-defined, and any additional members are locale-specific.

简而言之,您的问题的答案在您的编译器文档中。然而:

2.2. 2/ The character designated by the universal-character-name \UNNNNNNNN is that character whose character short name in ISO/IEC 10646 is NNNNNNNN; the character designated by the universal-character-name \uNNNN is that character whose character short name in ISO/IEC 10646 is 0000NNNN. If the hexadecimal value for a universal character name is less than 0x20 or in the range 0x7F-0x9F (inclusive), or if the universal character name designates a character in the basic source character set, then the program is illformed.

因此您可以保证您命名的字符被翻译成实现定义的编码,可能是特定于语言环境的。

关于c++ - 打印通用字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7081367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com