Search code examples
c++language-implementation

Does the range for integer values of a char depend on implementation?


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.


Solution

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