Why stores 255 in a char variable give its value -1 in C?

允我心安 提交于 2019-12-09 01:52:32

问题


I am reading a C book, and there is a text the author mentioned:

"if ch (a char variable) is a signed type, then storing 255 in the ch variable gives it the value -1".

Can anyone elaborate on that?


回答1:


Assuming 8-bit chars, that is actually implementation-defined behaviour. The value 255 cannot be represented as a signed 8-bit integer.

However, most implementations simply store the bit-pattern, which for 255 is 0xFF. With a two's-complement interpretation, as a signed 8-bit integer, that is the bit-pattern of -1. On a rarer ones'-complement architecture, that would be the bit pattern of negative zero or a trap representation, with sign-and-magnitude, it would be -127.

If either of the two assumptions (signedness and 8-bit chars) doesn't hold, the value will be¹ 255, since 255 is representable as an unsigned 8-bit integer or as a signed (or unsigned) integer with more than 8 bits.

¹ The standard guarantees that CHAR_BIT is at least 8, it may be greater.




回答2:


That is not guaranteed behavior. To quote ANSI/ISO/IEC 9899:1999 §6.3.1.3 (converting between signed and unsigned integers) clause 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.

I'll leave the bitwise/2's complement explanations to the other answers, but standards-compliant signed chars aren't even guaranteed to be too small to hold 255; they might work just fine (giving the value 255.)




回答3:


Try it in decimal. Suppose we can only have 3 digits. So our unsigned range is 0 - 999.

Let's see if 999 can actually behave as -1 (signed):

42 + 999 = 1041

Because we can only have 3 digits, we drop the highest order digit (the carry):

041 = 42 - 1

This is a general rule that applies to any number base.




回答4:


That's how two's complement works. Read all about it here.




回答5:


You have classical explanation in others messages. I give you a rule:

In a signed type with size n, presence of MSB set as 1, must interpreted as -2^(n-1).

For this concrete question, assuming size of char is 8 bits length (1 bytes), 255 to binary is equal to:

1*2^(7) +  
1*2^(6) + 
1*2^(5) + 
1*2^(4) + 
1*2^(3) + 
1*2^(2) + 
1*2^(1) + 
1*2^(0) = 255

255 equivalent to 1 1 1 1 1 1 1 1.

For unsigned char, you get 255, but if you are dealing with char (same as signed char), MSB represents a negative magnitude:

-1*2^(7) +  
1*2^(6) + 
1*2^(5) + 
1*2^(4) + 
1*2^(3) + 
1*2^(2) + 
1*2^(1) + 
1*2^(0) = -1


来源:https://stackoverflow.com/questions/10264344/why-stores-255-in-a-char-variable-give-its-value-1-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!