Why the range of signed char
is different in C and C++ ?
In C the range is -128 t0 127
and in C++ it is `-127 to 127' but on the other hand the unsigned ranges are same. why ?

- 46
- 1
- 6
-
12Who told you this? Because say this to them ... "No." – Fantastic Mr Fox Sep 20 '16 at 20:22
-
1Possible duplicate of http://stackoverflow.com/questions/3898688/range-of-signed-char – Fabulous Sep 20 '16 at 20:25
-
I already checked and then I asked this question. @simrandhamija – Devesh Pratap Singh Sep 20 '16 at 20:29
-
Yeah , they are , I have been reading C++ with Bjarne Stroustrup and its clearly mentioned over there ($6.2.3) @Ben – Devesh Pratap Singh Sep 20 '16 at 20:30
-
4Related: http://stackoverflow.com/questions/8010040/why-is-schar-min-defined-as-127-in-c99 – John Bollinger Sep 20 '16 at 20:31
-
THANKS @JohnBollinger – Devesh Pratap Singh Sep 20 '16 at 20:41
-
2@DeveshPratapSingh: That is not the C++ standard, so whatever it says is not binding to what C++ the language is. Voted to close as unclear since you asked with incorrect assumptions, without citing any sources, and then asked why those assumptions were what they were. – GManNickG Sep 20 '16 at 20:48
3 Answers
You are mistaken. The guaranteed minimum value for signed char is in fact -127 in both languages. Quote from C11 standard (draft N1570):
minimum value for an object of type signed char
SCHAR_MIN -127
Do note that the actual absolute minimum value is implementation defined and may be greater:
... Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.
And on processors that have 8 bit byte and two's complement representation (which is rather common), the minimum value will almost certainly be -128 in both C and C++. The reason why only -127 is guaranteed, is because it allows a non two's complement representation to be supported.
The text referenced in the comments, refers to the fact that the c++
standard does not guide how to implement signed char, ie. it can be implemented as 1's complement giving you the value -127 -> 127 (with +0 and -0).
The 256 values represented by an 8-bit byte can be interpreted as the values 0 to 255 or as the values −127 to 127 . No, not −128 to 127 as one might expect: the c++ standard leaves open the possibility of ones-compliment hardware and eliminates one value, thus the use of -128 is non-portable.
It doesn't say a char is -127 -> 127, it says it might be. Yes, if you want portability with every standard conforming system, you need to consider the possibility of a 1's complement implementation of char
. In reality, no-one ever has this ...
You should be more worried about things like how some hardware implements a 16 bit char ...

- 32,495
- 27
- 95
- 175
-
Thanks for your time dude! and the answer to your last line is
. – Devesh Pratap Singh Sep 20 '16 at 20:46
I'm not an expert, but I can't find a reference to the c++ range being -127 to 127. Everything I see says it is also -128 to 127. Reference: https://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.80).aspx

- 49
- 1
- 5
-
here comes the text - "The 256 values represented by an 8-bit byte can be interpreted as the values 0 to 255 or as the val- ues −127 to 127 . No, not −128 to 127 as one might expect ". This is what its written here, you can check yourself. I already mentioned the section and subsections above. – Devesh Pratap Singh Sep 20 '16 at 20:34
-
2-127 to 127 is the required *minimum* range. It is allowed to have a wider range. – Bo Persson Sep 20 '16 at 20:35
-
Sorry, -1. You can't generally answer C++ language questions by link to documentation for a specific compiler. My compiler's signed `char` has a range of -127 to 127, which is perfectly legal C++. – GManNickG Sep 20 '16 at 20:53
-
@GManNickG: Yes, it's perfectly legal -- but it's surprising. What compiler uses that range? Assuming a 2's-complement representation, what happens if you do `signed char sc = -127; --sc; std::cout << "sc = " << (int)sc << "\n";`? (I'm aware the behavior is undefined.) – Keith Thompson Sep 20 '16 at 21:15
-
1