[英]Can someone tell me why this program prints -56?
This code prints -56
and I'm not sure why 这段代码显示
-56
,我不确定为什么
#include<stdio.h>
main()
{
printf("%d\n", ((int)(char)200));
}
Because char
implicitly is signed char
on your platform. 因为
char
在您的平台上是隐式signed char
。
Do 做
printf("%d\n", ((int)(unsigned char)200));
to get 要得到
200
The type char
is inconsistent towards the rest of the C language, since char
can be either signed or unsigned. char
类型与C语言的其余部分不一致,因为char
可以是有符号的也可以是无符号的。 The C standard states it as implementation-defined behavior, meaning that the compiler can implement it in either way. C标准将其声明为实现定义的行为,这意味着编译器可以以任何一种方式实现它。 Your particular compiler seems to implement
char
as equal to signed char
. 您的特定编译器似乎将
char
实现为与signed char
相等。
Because of the above, char
should never be used for anything else but strings. 由于上述原因,除了字符串之外,
char
绝不能用于其他任何内容。 It is bad practice to use for arithmetic operations. 用于算术运算是不好的做法。 Such code relies on impl.defined behavior.
这样的代码依赖于impl.defined行为。
The type int
is always equal to signed int
. int
类型始终等于signed int
。 This is required by the C standard. 这是C标准所必需的。
Thus on your specific system, the code you have written is equal to: (signed int)(signed char)200
. 因此,在您的特定系统上,您编写的代码等于:
(signed int)(signed char)200
。
When you attempt to store the value 200 in a variable equivalent to signed char
, it will overflow and get interpreted as -56 (on a two's complement system). 当您尝试将值200存储在等效于有
signed char
的变量中时,它将溢出并解释为-56(在二进制补码系统上)。
When you cast a signed char
containing -56 to a signed int
, you get -56. 将包含-56的带
signed char
为带signed int
,得到-56。
The number 200 is out of the value range for signed char
. 数字200超出有
signed char
的值范围。 It wraps around into the negative value range in a two's complement way. 它以二进制补码方式环绕到负值范围内。
Obviously the (implicit) signedness of char
is defined to be signed
on your platform. 显然,
char
的(隐式)签名定义为在您的平台上signed
。 Note that this is not mandated by the standard. 请注意,这不是标准要求的。 If it were
unsigned
, your program would print 200. For further reading on this topic: 如果
unsigned
,则程序将输出200。有关此主题的更多信息,请参见:
Obviously, the size of the char
type is 8 bits on your platform. 显然,在您的平台上,
char
类型的大小为8位。 The definition of this quantity is unspecified by the language specification as well. 语言规范也未指定此数量的定义。 If it were larger, the program may very well print 200, just as you expected.
如果更大,该程序可能会按您期望的那样打印200。 For further reading on this topic:
有关此主题的进一步阅读:
Also note, that it is not specified by the standard that integer types must use the two's complement binary representation. 还要注意,标准没有规定整数类型必须使用二进制补码表示。 On a different platform, the cast may even produce a completely different result.
在不同的平台上,演员表甚至可能产生完全不同的结果。 For further reading:
进一步阅读:
char
is signed
, and int
is signed
(by default on your platform). char
已signed
,而int
已signed
(默认情况下在您的平台上)。
(signed char)11001000
= -56 decimal (signed char)11001000
= -56十进制
(signed int)0000000011001000
= 200 decimal (signed int)0000000011001000
= 200十进制
Have a look at Signed number representations 看看带符号的数字表示
char是-128〜127的8位有符号,所以200超出了值范围
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.