range for integer values of chars in c++

2019-01-19 11:27发布

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.

5条回答
对你真心纯属浪费
2楼-- · 2019-01-19 11:47

You're stuck in two's complement thinking - The C++ standard does not define the representation used for negative numbers!

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.

查看更多
聊天终结者
3楼-- · 2019-01-19 11:51

It's wrong to think that an unsigned char ranges from 0 to 255. that's only its minimal range. a char must have at least 8 bits, and signed char, unsigned char and char itself can have indeed more that just 8 bits. so then that means that unsigned char could go beyond 255. though admittedly, i haven't got an implementation where it had more than 8 bits, it theoretically is possible. that's specified in the c89 standard (on which c++03 bases), documenting file limits.h (CHAR_BIT, UCHAR_MAX, CHAR_MAX).

查看更多
别忘想泡老子
4楼-- · 2019-01-19 11:54

Because the standard doesn't say anything about the char type, "char" can be:

  1. "unsigned char" (0-255) on some compilers (example: TexasInstruments compiler for their ARM processors - OMAP series)

  2. "signed char" (-128-127) on most compilers (gcc, MSVC ...)

To make sure you always have a well defined range you should use "signed char" or "unsigned char".

查看更多
干净又极端
5楼-- · 2019-01-19 12:02

Character types in C and C++

From reading that it seems it can be any of those, depending on implementation.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-19 12:05

Thanks Roddy and Esteban Brenes for the helpful replies.

This is my current understanding:

There are three possibilities. If the values are represented as unsigned, a char will range from 0 to 255. If the values are represented as signed in two's complement, a char will range from -128 to 127. Finally, if the values are represented as signed in one's complement, a char will range from -127 to 127. This final possibility suggests that there would only be 255 possible values in contrast to the 256 possible values for the first two implementations, but this fails to take into account the negative zero in one's complement representations.

查看更多
登录 后发表回答