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

5

15

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.

Jitters answered 4/11, 2008 at 20:47 Comment(2)
do you have any idea as to why the range of integer is from -128 to 127 and not -127 to 128??Interlocutor
Since C++20 signed numbers are two's compliment with the adaption of P1236r1. See also #71354523 . So chars typically either go from -128..127 or from 0..255. See also en.cppreference.com/w/cpp/language/types This page also says that since C++11 8 bit one's complement char was indirectly disallowed to support UTF-8 0x80. chars are between 8 and 64 bits wide.Octahedrite
D
18

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.

Donnenfeld answered 4/11, 2008 at 20:52 Comment(0)
I
6

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

Internship answered 5/11, 2008 at 0:38 Comment(1)
This is true. I've worked with embedded DSP processors where char, short, int, and long are all 32 bits.Deshawndesi
L
3

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

Lentha answered 5/11, 2008 at 19:57 Comment(0)
H
1

Character types in C and C++

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

Hyalite answered 4/11, 2008 at 21:6 Comment(0)
J
0

My current understanding is that 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.

Jitters answered 4/11, 2008 at 22:49 Comment(1)
Almost: unsigned goes from 0 to 255 not 256. I've never worked on a 1's complement machine so exactly how +0 and -0 work is a guess. I'd expect from a C perspective you could never tell the differenceDonnenfeld

© 2022 - 2024 — McMap. All rights reserved.