【发布时间】:2014-12-27 22:51:22
【问题描述】:
考虑 C 中的signed char:
-128 represents what character?
-127 represents what character?
...
-1 represents what character?
0 represents the NULL character, right?
1 represents the SOH character, right?
...
127 represents the DEL character, right?
接下来,考虑 C 中的unsigned char:
0 represents the NULL character, right?
1 represents the SOH character, right?
...
127 represents the DEL character, right?
128 represents what character?
129 represents what character?
...
255 represents what character?
signed char中0到127所代表的字符序列是否与0 到 127 在unsigned char?
signed char中-128到-1表示的字符序列是不是同一个序列unsigned char中由 128 到 255 表示的字符数?
signed char 可以代表 256 个不同的字符,对吧?
unsigned char 可以代表 256 个不同的字符,对吧?
谢谢!
【问题讨论】:
-
只要编译器以二进制补码表示法使用 8 位
char值,您建议的范围就是正确的。周围可能仍然有一个人的补码或符号幅度系统,但您相对不太可能找到它们。例如,有些系统具有 36 位字,其中字符为 9 位。一些 DSP 系统使用 16 位字符类型。在这两种情况下,您的假设都偏离了方向,但它们非常专业。关于字符的映射,C标准唯一固定的是NUL'\0',即字符码0;所有其他的都是实现定义的。 -
@Jonathan Leffler 不错! - 次要澄清:
NUL是 C“空字符”'\0'的 ASCII 名称,而不是 C 规范名称。 -
@chux: 是的,你说得对 — §7.1.1 术语定义(用于库的介绍),标准使用“空字符”,NUL 是 ASCII 中的标准缩写对于零字节。 §6.4.5 字符串文字说“在翻译阶段 7,一个字节或值为零的代码被附加到由一个或多个字符串文字产生的每个多字节字符序列。78)”和脚注 78 说“字符串文字不需要一个字符串(参见 7.1.1),因为一个空字符可能通过
\0转义序列嵌入其中。'
标签: c gcc character-encoding byte unsigned-char