Devesh Pratap Singh Devesh Pratap Singh - 1 year ago 73
C Question

Why is the range of signed char is different in C and C++?

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 ?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download