minimal range of char, short int

Discussion in 'C++' started by Suresh V, Jul 4, 2010.

  1. Suresh V

    Suresh V Guest

    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 ?
    Suresh V, Jul 4, 2010
    #1
    1. Advertising

  2. Suresh V

    Bo Persson Guest

    Suresh V wrote:
    > 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
    Bo Persson, Jul 4, 2010
    #2
    1. Advertising

  3. Suresh V

    Öö Tiib Guest

    On 4 juuli, 15:19, Suresh V <> wrote:
    > 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.
    Öö Tiib, Jul 4, 2010
    #3
  4. Suresh V

    Suresh V Guest

    > 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 :)
    Suresh V, Jul 4, 2010
    #4
  5. Suresh V

    Rolf Magnus Guest

    Daniel T. wrote:

    > Suresh V <> wrote:
    >
    >> I am reading the book "The complete reference C++" i see that minimal
    >> range of char has been mentioned as -127 to 127

    >
    > 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.
    Rolf Magnus, Jul 4, 2010
    #5
  6. Suresh V

    red floyd Guest

    On Jul 4, 5:19 am, Suresh V <> wrote:
    > I am reading the book "The complete reference C++"


    If that is the book by Schildt, burn it and buy a good book.
    It's full of inaccuracies.

    See http://www.accu.org for suggestions on good references.
    red floyd, Jul 6, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,190
    Martien Verbruggen
    Jun 27, 2003
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    389
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,605
    Old Wolf
    Jan 20, 2004
  4. Range of short int?

    , Dec 24, 2005, in forum: C Programming
    Replies:
    12
    Views:
    588
  5. gert
    Replies:
    20
    Views:
    1,123
Loading...

Share This Page