undefined behavior?

Discussion in 'C Programming' started by Mantorok Redgormor, Oct 15, 2003.

  1. If I do something like the following:

    unsigned int bar=10;

    Then use, -bar with the unary '-' and assign this value to an int,
    does this invoke undefined behavior in anyway?

    Basically I have:
    unsigned int bar=10;
    int foo;
    foo = -bar; /* Undefined behavior? */
    I'm just wondering if this value that is expressed in the expression
    -bar can be representable at all times with the int variable and in no
    way ends up invoking undefined behavior
     
    Mantorok Redgormor, Oct 15, 2003
    #1
    1. Advertisements

  2. Mantorok Redgormor

    cody Guest

    If I do something
    like the following:

    " 6.3.1.3 Signed and unsigned integers

    [#1] 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.

    [#2] 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.

    [#3] Otherwise, the new type is signed and the value cannot
    be represented in it; the result is implementation-defined."


    Well, not undefined, but implementation defined.
     
    cody, Oct 16, 2003
    #2
    1. Advertisements

  3. First, "-bar" will give a very large positive number, and not a negative
    number. If unsigned int = 32 bit then -bar equals 2^32 - 10.

    Second, if that number is too large to fit into an unsigned int, then
    the behavior is "implementation defined". So your compiler should really
    document what will happen. It is defined behaviour, so the program will
    not crash.
     
    Christian Bau, Oct 16, 2003
    #3
  4. Consider this code:

    unsigned int bar = UINT_MAX;
    int foo = -bar;

    The value is unrepresentable as a signed int, so the result is
    implementation-defined (see 3.2.1 of the ANSI C Standard).
     
    Richard Heathfield, Oct 16, 2003
    #4
  5. Mantorok Redgormor

    Jack Klein Guest

    Not a chance, assigning an integer expression, literal, or value to
    any unsigned integer type is never "implementation-defined".

    The value is exactly defined, although the resulting value is
    implementation-defined, because it depends on the maximum value of the
    unsigned destination type is implementation-defined.

    --
    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++ ftp://snurse-l.org/pub/acllc-c++/faq
     
    Jack Klein, Oct 16, 2003
    #5
  6. Mantorok Redgormor

    Richard Bos Guest

    But foo is a _signed_ integer. It is not the validity of -bar that is
    questioned, but the validity of assigning -bar to a signed int.

    Richard
     
    Richard Bos, Oct 16, 2003
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.