以一段伪代码开始:
char a = 0x80;
unsigned short b;
b = (unsigned short)a;
printf ("0x%04x\r\n", b); // => 0xff80
根据我目前的理解,“char”根据定义既不是有符号字符也不是无符号字符,而是第三种类型的符号。
“a”首先从(可能与平台相关)8 位存储符号扩展到(可能又是平台特定的)16 位有符号短整数,然后转换为无符号短整数,这是怎么回事?
是否有一个c标准来决定展开的顺序?
该标准是否以任何方式指导如何处理“纯”字符的第三种类型的符号(我曾经称它为 X-char,x 表示未确定的符号)以便结果至少是确定性的?
PS:如果在赋值行的'a'前插入一个"(unsigned char)"语句,那么打印行的结果确实变成了0x0080。因此,只有连续两次类型转换才能提供某些意图的预期结果。
最佳答案
char
类型不是“第三”符号。它是 signed char
或 unsigned char
,具体是哪一个由实现定义。
这是由 C standard 的第 6.2.5p15 节规定的:
The three types
char
,signed char
, andunsigned char
are collectively called the character types. The implementation shall definechar
to have the same range, representation, and behavior as eithersigned char
orunsigned char
.
看起来在您的实现中,char
与 signed char
相同,因此因为该值为负数并且因为目标类型是无符号的,所以必须对其进行转换。
第 6.3.1.3 节规定了整数类型之间的转换是如何发生的:
1 When a value with integer type is converted to another integer type other than _Bool ,if the value can be represented by the new type, it is unchanged.
2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
由于值 0x80 == -128 不能用 unsigned short
表示,因此发生第 2 段中的转换。
关于c: 将 char 值类型转换为 unsigned short,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53282305/