comparison between signed and unsigned int

Discussion in 'C Programming' started by compcreator@gmail.com, Sep 21, 2007.

  1. Guest

    I have tried the following program. The problem is it is printing
    False

    I checked values for a and b but there is something wrong with the
    comparison.
    I thing it might be because of signed and unsigned conversion.
    Either b is upgraded to unsigned and the resulting value during
    comparison is > 5 or a is downgraded to signed and the resulting value
    is lower than -1.

    void main()
    {
    unsigned int a = 5;
    signed int b = -1;

    if(b <= a)
    printf("True");
    else
    printf("False");
    }

    Can somebody explain why is this happening....?
    , Sep 21, 2007
    #1
    1. Advertising

  2. Tim Prince Guest

    wrote:
    > I have tried the following program. The problem is it is printing
    > False
    >
    > I checked values for a and b but there is something wrong with the
    > comparison.
    > I thing it might be because of signed and unsigned conversion.
    > ... b is upgraded to unsigned and the resulting value during
    > comparison is > 5

    Yes, this is an FAQ and should be explained in all textbooks.
    Tim Prince, Sep 21, 2007
    #2
    1. Advertising

  3. Ben Pfaff Guest

    writes:

    > unsigned int a = 5;
    > signed int b = -1;
    >
    > if(b <= a)


    When you compare an unsigned int and a signed int in this
    fashion, the signed int is converted to unsigned int. Converting
    a negative signed int to a unsigned int is done by adding
    UINT_MAX + 1. Thus, the comparison is effectively:
    if (UINT_MAX <= 5)
    which is false.
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Sep 21, 2007
    #3
  4. Mark Bluemel Guest

    wrote:
    > I have tried the following program. The problem is it is printing
    > False
    >
    > I checked values for a and b but there is something wrong with the
    > comparison.
    > I thing it might be because of signed and unsigned conversion.


    FAQs 3.18 and 3.19 - look under http://c-faq.com
    Mark Bluemel, Sep 21, 2007
    #4
  5. writes:
    > I have tried the following program. The problem is it is printing
    > False
    >
    > I checked values for a and b but there is something wrong with the
    > comparison.
    > I thing it might be because of signed and unsigned conversion.
    > Either b is upgraded to unsigned and the resulting value during
    > comparison is > 5 or a is downgraded to signed and the resulting value
    > is lower than -1.
    >
    > void main()
    > {
    > unsigned int a = 5;
    > signed int b = -1;
    >
    > if(b <= a)
    > printf("True");
    > else
    > printf("False");
    > }
    >
    > Can somebody explain why is this happening....?


    In addition to reading the FAQ, you need to change 'void main()' to
    'int main(void)', add '#include <stdio.h>', add a terminating newline
    to each of your output strings (or use puts()), and add a 'return 0;'
    at the end of your program.

    You should also find out how to persuade your compiler to give you
    more warnings; it could have told you about most of these problems.

    --
    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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 21, 2007
    #5
  6. CBFalconer Guest

    wrote:
    >
    > I have tried the following program. The problem is it is printing
    > False
    >

    .... snip ...
    >
    > void main() {
    > unsigned int a = 5;
    > signed int b = -1;
    >
    > if (b <= a) printf("True");
    > else printf("False");
    > }
    >
    > Can somebody explain why is this happening....?


    a is signed and negative, and cannot fit into the range covered by
    b. Therefore it is converted to an unsigned value before
    comparing. The conversion results in UINT_MAX, which is
    considerably larger than 5. Signed can always be converted to
    unsigned, but not the reverse.

    Get rid of the 'void main()', which marks you as unknowing. main
    returns an int, say and do so. The satisfactory return values are
    0, EXIT_SUCCESS, and EXIT_FAILURE. The latter two require #include
    <stdlib.h>. Also specify void in the parameter list, unless you
    are using argc and argv.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Sep 21, 2007
    #6
  7. Army1987 Guest

    On Fri, 21 Sep 2007 06:16:07 -0700, compcreator wrote:

    > I have tried the following program. The problem is it is printing
    > False
    >
    > I checked values for a and b but there is something wrong with the
    > comparison.
    > I thing it might be because of signed and unsigned conversion.
    > Either b is upgraded to unsigned and the resulting value during
    > comparison is > 5 or a is downgraded to signed and the resulting value
    > is lower than -1.

    Last time I checked 5 wasn't lower than -1.
    --
    Army1987 (Replace "NOSPAM" with "email")
    If you're sending e-mail from a Windows machine, turn off Microsoft's
    stupid “Smart Quotes†feature. This is so you'll avoid sprinkling garbage
    characters through your mail. -- Eric S. Raymond and Rick Moen
    Army1987, Sep 21, 2007
    #7
    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. John Buckley

    comparison between signed and unsigned

    John Buckley, Oct 23, 2003, in forum: C Programming
    Replies:
    2
    Views:
    464
    Dan Pop
    Oct 24, 2003
  2. Alex
    Replies:
    3
    Views:
    638
    Michael Mair
    Apr 26, 2006
  3. Gary Wessle
    Replies:
    4
    Views:
    606
    Ian Collins
    Jul 29, 2006
  4. Replies:
    4
    Views:
    376
  5. pozz
    Replies:
    12
    Views:
    717
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page