unsigned variables

Discussion in 'C Programming' started by Christian Christmann, May 7, 2006.

  1. Hi,

    what does the ANSI C-99 standard says when an unsigned variable
    is assigned a negative number?, i.e.

    unsigned char a = -100;


    Is this undefined behavior or should the negative number be casted
    to a unsigned value?

    Thank you.

    Chris
    Christian Christmann, May 7, 2006
    #1
    1. Advertising

  2. In article <445e2e51$0$4494$-online.net>,
    Christian Christmann <> wrote:
    >what does the ANSI C-99 standard says when an unsigned variable
    >is assigned a negative number?, i.e.


    >unsigned char a = -100;


    >Is this undefined behavior or should the negative number be casted
    >to a unsigned value?


    The behaviour is well defined: (UCHAR_MAX+1) will be added to
    the negative quantity sufficient times to arrive at a positive
    quantity.


    In 2's complement arithmetic, the end result is the same bit
    pattern for signed char a = -100 and unsigned char a = -100,
    but the standard is written the way it is so as not to assume
    2's complement.
    --
    There are some ideas so wrong that only a very intelligent person
    could believe in them. -- George Orwell
    Walter Roberson, May 7, 2006
    #2
    1. Advertising

  3. Christian Christmann

    Michael Mair Guest

    Christian Christmann schrieb:
    > Hi,
    >
    > what does the ANSI C-99 standard says when an unsigned variable
    > is assigned a negative number?, i.e.
    >
    > unsigned char a = -100;


    The conversion "integer->unsigned integer" is done adding or subtracting
    the unsigned integer's maximal representable value plus one sufficiently
    often to arrive in the range of the unsigned integer type.
    In this case, this will effectively amount to
    unsigned char a = (UCHAR_MAX + 1) - 100;

    > Is this undefined behavior


    No.
    The conversions "floating point->signed or unsigned integer" or
    "floating point->floating point" lead to UB if the value cannot be
    represented in the target type.
    I.e.
    unsigned char a = -100.0;
    is not necessarily the same as the integer version.
    A safe version of this conversion is
    int b = -100.0; /* convert to integer type which can represent
    ** the value */
    unsigned char a = b;


    > or should the negative number be casted to a unsigned value?


    A cast is an explicit conversion. The result is the same as for
    an implicit conversion, i.e.
    unsigned char a = (unsigned char) -100;
    leads to the same result as
    unsigned char a = -100;
    Note: Sometimes, casts are used to "shut up the compiler" in
    situations like this; this is not necessarily a good idea.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, May 7, 2006
    #3
    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. Timo Freiberger
    Replies:
    3
    Views:
    942
    Bob Hairgrove
    Oct 30, 2004
  2. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    674
    Eric Sosman
    Jul 8, 2003
  3. Replies:
    1
    Views:
    450
    Diez B. Roggisch
    Jun 1, 2005
  4. sridhar

    comparing unsigned long and unsigned int

    sridhar, Nov 1, 2004, in forum: C Programming
    Replies:
    6
    Views:
    445
    J. J. Farrell
    Nov 3, 2004
  5. pozz
    Replies:
    12
    Views:
    738
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page