minimal range of char, short int

S

Suresh V

I am reading the book "The complete reference C++" i see that minimal
range of char has been mentioned as -127 to 127 similarly for short
int it is mentioned as -32767 to -32767.

But i try to assign

char ch = 128;
short int i = 32768;

printf ("ch = %d", ch); \\ prints ch = -128
printf ("ch = %d", i); \\ prints ch = -32768

I got confused after seeing the result. How come it is possible to
print even -128 if minimal range is (-127 ot 127) same applies to
short int ?
 
B

Bo Persson

Suresh said:
I am reading the book "The complete reference C++" i see that
minimal range of char has been mentioned as -127 to 127 similarly
for short int it is mentioned as -32767 to -32767.

But i try to assign

char ch = 128;
short int i = 32768;

printf ("ch = %d", ch); \\ prints ch = -128
printf ("ch = %d", i); \\ prints ch = -32768

I got confused after seeing the result. How come it is possible to
print even -128 if minimal range is (-127 ot 127) same applies to
short int ?

It obviously can have more that the minimum range. :)

On current hardware, it is common to have one extra negative value
giving char a range of -128 to +127. That's affecting the result here.

To be really strict, you are not allowed by the standard to use out of
range values for signed integers. Overflow causes undefined behavior
according to the standard. That allows the implementation to produce
any result it feels like, perhaps displaying the most negative value.


Bo Persson
 
Ö

Öö Tiib

I am reading the book "The complete reference C++" i see that minimal
range of char has been mentioned as -127 to 127 similarly for short
int it is mentioned as -32767 to -32767.

But i try to assign

char ch = 128;
short int i = 32768;

printf ("ch = %d", ch); \\ prints ch = -128
printf ("ch = %d", i); \\ prints ch = -32768

I got confused after seeing the result. How come it is possible to
print even -128 if minimal range is (-127 ot 127) same applies to
short int ?

The book either lies or you did not understand what it did say.
Exact range for char is: std::numeric_limits<char>::min() to
std::numeric_limits<char>::max().
Exact range for short is: std::numeric_limits<short>::min() to
std::numeric_limits<short>::max().

These numeric_limits may differ from platform to platform. Note that
printf is bad thing for such tests since it does not care about type
of arguments. With %d format specifier of printf you get whatever
argument (or slice of it) reinterpreted as int.
 
S

Suresh V

It obviously can have more that the minimum range.  :)
On current hardware, it is common to have one extra negative value
giving char a range of -128 to +127. That's affecting the result here.

To be really strict, you are not allowed by the standard to use out of
range values for signed integers. Overflow causes undefined behavior
according to the standard. That allows the implementation to produce
any result it feels like, perhaps displaying the most negative value.

Bo Persson

C++ data types has lot of dependencies on implementation(platform)
from assigning values to allocating size for each type :)
 
R

Rolf Magnus

Daniel said:
That is incorrect. A char is only guaranteed to be able to hold from 0
to 127,

No. The range is required to be bigger, but that's the range that you can
rely on if you don't want to depend on implementation-defined behavior.
it may hold more of course.

It will _definitely_ be able to hold more.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top