简体   繁体   English

有人可以告诉我为什么该程序打印-56吗?

[英]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). charsigned ,而intsigned (默认情况下在您的平台上)。

(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.

相关问题 有人能告诉我为什么这个程序总是崩溃吗? - Can someone tell me why this program keeps crashing? 有人能告诉我为什么我的chain1在我第二次使用它时突然打印出99吗? :) - Can someone pls tell me why my chain1 suddenly prints 99 the second time i use it ? :) 有人能告诉我为什么数学没有打印吗? - Can someone tell me why the math is not printing? 在不使用 3rd(临时变量)的情况下进行交换在这个程序中给出了不同的答案,有人可以告诉我为什么吗? - swapping without using 3rd(temporary variable) is giving diff answer in this program can someone tell me why? 有人可以告诉我为什么这不起作用吗? - Can someone tell me why this isn't working? 有人能告诉我为什么函数没有返回值吗? 新来的 C - Can someone tell me why the functions are not returning a value? New to C 有人可以帮我为什么我的程序打印随机数吗? - Can someone help me why my program is printing a random number? 有人可以告诉我分段错误的原因吗? - Can Someone tell me the reason of the segmentation fault - (core dumped) error in this program 有人可以使用fork()系统调用告诉我我的程序怎么了(编程C) - can someone tell me what's wrong with my program using fork() system calls(Programming C) 有人可以告诉我我的错误在哪里吗? 我的程序输出错误 - Can someone tell me where's my mistake? My program gives the wrong output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM