问题描述
我正在阅读 C ++编程语言,其中Stroustrup指出char的int值范围可以从0到255或-127到127,具体取决于实现方式。这样对吗?似乎应该在-128到127之间。如果没有,为什么在第二个实现可能性中它们只有255个可能的值,而不是256个。
I'm reading The C++ Programming Language and in it Stroustrup states that the int value of a char can range from 0 to 255 or -127 to 127, depending on implementation. Is this correct? It seems like it should be from -128 to 127. If not, why are their only 255 possible values in the second implementation possibility, not 256.
推荐答案
您陷入了的思考-C ++标准
You're stuck in two's complement thinking - The C++ standard does not define the representation used for negative numbers!
如果您的计算机(禁止使用上帝)使用代表负数,您在8位字节中的范围是-127到+ 127。从好的方面来说,您有两种可能的零表示形式。
If your computer (god forbid) uses ones's complement to represent negative numbers, you have a range of -127 to + 127 in an 8-bit byte. On the upside, you have two different possible representations for zero...
但是,在现实世界中,。
However, in the real world, you're unlikely to meet a one's complement computer.
这篇关于C ++中char整数值的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!