-1

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 ?

3 Answers3

11

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.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
5

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

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
-2

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

BenG
  • 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
    @KeithThompson: ...I admit, it was my brain compiler. :) – GManNickG Sep 20 '16 at 21:17