[ACCEPTED]-range for integer values of chars in c++-language-details

Accepted answer
Score: 18

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

If your computer (god 5 forbid) uses ones's complement to represent negative numbers, you 4 have a range of -127 to + 127 in an 8-bit 3 byte. On the upside, you have two different 2 possible representations for zero...

However, in 1 the real world, you're unlikely to meet a one's complement computer.

Score: 5

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

Score: 3

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

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

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

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

Score: 1

Character types in C and C++

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

Score: 0

Thanks Roddy and Esteban Brenes for the 14 helpful replies.

This is my current understanding:

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

More Related questions