unsigned wierdness

Discussion in 'C Programming' started by tobiasoed@hotmail.com, May 2, 2005.

  1. Guest

    Hello!
    I have the following piece of code:

    #include <stdio.h>

    int main(void){

    unsigned int u;
    int i1, i2;
    double d1,d2;

    u = 0;

    i1 = u - 1;
    i2 = (signed int) u - 1;

    d1 = u - 1;
    d2 = (signed int) u - 1;

    printf("i1 = %d, i2 = %d, d1 = %f, d2 = %f\n",i1,i2,d1,d2);
    }

    and don't understand the output:
    tobsbox:~/C$ cc -std=c99 -Wall -W -pedantic unsigned.c && ./a.out
    i1 = -1, i2 = -1, d1 = 4294967295.000000, d2 = -1.000000

    It looks like u is promoted to signed int for i2 but not d2. Is
    this normal or is it a compiler bug?
    Tobias
    ps: My compiler is
    cc (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.fc3)
    Copyright (C) 2004 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is
    NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
    PURPOSE.
     
    , May 2, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    >
    > It looks like u is promoted to signed int for i2 but not d2.


    Ooups, I meant i1 and d1 of course
    Tobias
     
    , May 2, 2005
    #2
    1. Advertising

  3. Guest

    wrote:
    > Hello!
    > I have the following piece of code:
    >
    > #include <stdio.h>
    >
    > int main(void){
    >
    > unsigned int u;
    > int i1, i2;
    > double d1,d2;
    >
    > u = 0;
    >
    > i1 = u - 1;
    > i2 = (signed int) u - 1;
    >
    > d1 = u - 1;
    > d2 = (signed int) u - 1;
    >
    > printf("i1 = %d, i2 = %d, d1 = %f, d2 = %f\n",i1,i2,d1,d2);
    > }
    >
    > and don't understand the output:
    > tobsbox:~/C$ cc -std=c99 -Wall -W -pedantic unsigned.c && ./a.out
    > i1 = -1, i2 = -1, d1 = 4294967295.000000, d2 = -1.000000


    Never mind. The whole rhs is done in unsigned arithmetic and gives
    -1U which is 4294967295 here. The assignement to i1 'overflows' and
    happens to give -1. The assignement to d1 doesn't and preserves the
    value. I mistakenly thought that the rhs would all be done in
    signed.
    Tobias.
     
    , May 2, 2005
    #3
  4. Old Wolf Guest

    wrote:
    > wrote:
    > >
    > > int main(void){
    > >
    > > unsigned int u;
    > > int i1, i2;
    > > double d1,d2;
    > >
    > > u = 0;
    > > i1 = u - 1;
    > > i2 = (signed int) u - 1;
    > > d1 = u - 1;
    > > d2 = (signed int) u - 1;
    > > printf("i1 = %d, i2 = %d, d1 = %f, d2 = %f\n",i1,i2,d1,d2);
    > > }
    > >
    > > i1 = -1, i2 = -1, d1 = 4294967295.000000, d2 = -1.000000

    >
    > Never mind. The whole rhs is done in unsigned arithmetic and
    > gives -1U which is 4294967295 here. The assignement to i1
    > 'overflows' and happens to give -1.


    To be precise: since 4294967295 is outside the range of
    signed int, the assignment to i1 either gives an
    implementation-defined result, or raises an implementation-
    defined signal.
     
    Old Wolf, May 2, 2005
    #4
    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. Terry Olsen

    Datagrid Cell Spacing Wierdness

    Terry Olsen, Mar 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    584
    Terry Olsen
    Mar 29, 2005
  2. danthman

    Wierdness with "Imports" statement

    danthman, Dec 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,200
    danthman
    Dec 16, 2005
  3. Larry
    Replies:
    0
    Views:
    408
    Larry
    Feb 17, 2006
  4. VisionSet
    Replies:
    0
    Views:
    401
    VisionSet
    Aug 29, 2004
  5. pozz
    Replies:
    12
    Views:
    792
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page