overflow

Discussion in 'C Programming' started by Christian Christmann, Sep 21, 2005.

  1. Hi,

    I would like to hear your opinion about the C standards.

    When I have a

    signed char a = 130;

    I explicitly generate an overflow since the maximum value a singned char
    can accommodate is "127". Is this behavior of the overflow defined in any
    way or do I get an undefined state of "a" due to the C standards?

    Thank you
    Chris
     
    Christian Christmann, Sep 21, 2005
    #1
    1. Advertising

  2. Christian Christmann

    pete Guest

    Christian Christmann wrote:
    >
    > Hi,
    >
    > I would like to hear your opinion about the C standards.
    >
    > When I have a
    >
    > signed char a = 130;
    >
    > I explicitly generate an overflow
    > since the maximum value a singned char
    > can accommodate is "127".


    That's the minimum maximum value.
    SCHAR_MAX is higher than that, if CHAR_BIT is greater than 8.

    > Is this behavior of the overflow defined in any way


    That's not overflow.
    That's the assignment of an out of range value.

    > or do I get an undefined state of "a" due to the C standards?


    Overflow must be the result of an arithmetic operation.

    This is overflow:

    signed char a = SCHAR_MAX;

    ++a;

    Undefined behavior of part of the code, means that
    the behavior of the entire program is undefined.

    --
    pete
     
    pete, Sep 21, 2005
    #2
    1. Advertising

  3. Christian Christmann

    Guest

    oh,please test a=255;and a=256,you will found that it's not overflow
    but show you a wrong glance.
     
    , Sep 21, 2005
    #3
  4. Christian Christmann

    Tim Rentsch Guest

    pete <> writes:

    > This is overflow:
    >
    > signed char a = SCHAR_MAX;
    >
    > ++a;


    That's not overflow either, unless SCHAR_MAX == INT_MAX.
     
    Tim Rentsch, Sep 21, 2005
    #4
  5. Christian Christmann <> writes:
    > I would like to hear your opinion about the C standards.
    >
    > When I have a
    >
    > signed char a = 130;
    >
    > I explicitly generate an overflow since the maximum value a singned char
    > can accommodate is "127". Is this behavior of the overflow defined in any
    > way or do I get an undefined state of "a" due to the C standards?


    Assuming SCHAR_MAX==127 (which is common but not universal), the
    expression 130 is of type int, so it's implicitly converted to type
    signed char. To find out what happens, we have to read C99 6.3.1.3,
    which covers conversion of signed and unsigned integers:

    When a value with integer type is converted to another integer
    type other than _Bool, if the value can be represented by the
    new type, it is unchanged.

    Otherwise, if the new type is unsigned, the value is converted
    by repeatedly adding or subtracting one more than the maximum
    value that can be represented in the new type until the value is
    in the range of the new type.

    Otherwise, the new type is signed and the value cannot be
    represented in it; either the result is implementation-defined
    or an implementation-defined signal is raised.

    The third paragraph applies here; either an implementation-defined
    value is assigned to a, or an implementation-defined signal is raised.
    (The wording about signals is new in C99.)

    It's likely that the implementation-defined value will be -126, but
    the standard doesn't guarantee that.

    The rules are different for conversions and for arithmetic operations.
    If an arithmetic operation overflows for a signed type, the behavior
    is undefined.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 21, 2005
    #5

  6. > The third paragraph applies here; either an implementation-defined value
    > is assigned to a, or an implementation-defined signal is raised. (The
    > wording about signals is new in C99.)



    Thank you very much. This was an illuminating answer and help
    me a lot.

    Regards,
    Chris
     
    Christian Christmann, Sep 21, 2005
    #6
  7. Christian Christmann

    pete Guest

    Tim Rentsch wrote:
    >
    > pete <> writes:
    >
    > > This is overflow:
    > >
    > > signed char a = SCHAR_MAX;
    > >
    > > ++a;

    >
    > That's not overflow either, unless SCHAR_MAX == INT_MAX.


    Good point!

    This is overflow:

    int a = INT_MAX;

    ++a;

    --
    pete
     
    pete, Sep 21, 2005
    #7
  8. Christian Christmann

    Jack Klein Guest

    On 21 Sep 2005 00:56:05 -0700, ""
    <> wrote in comp.lang.c:

    > oh,please test a=255;and a=256,you will found that it's not overflow
    > but show you a wrong glance.


    Please don't post nonsense in this group, with improper grammar,
    punctuation, and capitalization to boot.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Sep 22, 2005
    #8
    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. Isaac
    Replies:
    2
    Views:
    3,858
    Arvind Kumar
    Aug 18, 2003
  2. Zaki
    Replies:
    2
    Views:
    6,606
    Egbert Molenkamp
    Jun 30, 2004
  3. Rune Christensen
    Replies:
    4
    Views:
    9,932
    David R Brooks
    Jan 22, 2005
  4. Nemesis

    Signed Adder without overflow

    Nemesis, May 24, 2005, in forum: VHDL
    Replies:
    4
    Views:
    7,543
    Nemesis
    May 25, 2005
  5. Lilmiss

    Overflow detector

    Lilmiss, Aug 2, 2005, in forum: VHDL
    Replies:
    1
    Views:
    2,447
    Ralf Hildebrandt
    Aug 2, 2005
Loading...

Share This Page