comparing unsigned long and unsigned int

Discussion in 'C Programming' started by sridhar, Nov 1, 2004.

  1. sridhar

    sridhar Guest

    #include <stdio.h>
    int main(){
    unsigned int ui = 0;
    if(0x0ul <= ui){
    printf("less eq\n");
    }
    }

    On my system unsigned long is 64 bits and unsigned int is 32.The
    compiler gives a warning

    warning: comparison is always true due to limited range of data type

    What is happening here?
    Thanks
    sridhar
     
    sridhar, Nov 1, 2004
    #1
    1. Advertising

  2. In article <>,
    (sridhar) wrote:

    > #include <stdio.h>
    > int main(){
    > unsigned int ui = 0;
    > if(0x0ul <= ui){
    > printf("less eq\n");
    > }
    > }
    >
    > On my system unsigned long is 64 bits and unsigned int is 32.The
    > compiler gives a warning
    >
    > warning: comparison is always true due to limited range of data type
    >


    The compiler is letting you know that 0ul <= ui is always true, since
    unsigned numbers can't be negative.

    Cheers,
    - jonathan
     
    Jonathan Adams, Nov 2, 2004
    #2
    1. Advertising

  3. (sridhar) writes:
    > #include <stdio.h>
    > int main(){
    > unsigned int ui = 0;
    > if(0x0ul <= ui){
    > printf("less eq\n");
    > }
    > }
    >
    > On my system unsigned long is 64 bits and unsigned int is 32.The
    > compiler gives a warning
    >
    > warning: comparison is always true due to limited range of data type


    You're not getting the warning because you're comparing ui to an
    unsigned long; you're getting the warning because you're comparing ui
    to zero.

    Think about it. ui is unsigned. For what possible values of ui would
    (0x0ul <= ui) or, more simply, (ui >= 0), be false?

    --
    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, Nov 2, 2004
    #3
  4. sridhar

    sridhar Guest

    Keith Thompson <> wrote in message news:<>...
    > (sridhar) writes:
    > > #include <stdio.h>
    > > int main(){
    > > unsigned int ui = 0;
    > > if(0x0ul <= ui){
    > > printf("less eq\n");
    > > }
    > > }
    > >
    > > On my system unsigned long is 64 bits and unsigned int is 32.The
    > > compiler gives a warning
    > >
    > > warning: comparison is always true due to limited range of data type

    >
    > You're not getting the warning because you're comparing ui to an
    > unsigned long; you're getting the warning because you're comparing ui
    > to zero.
    >
    > Think about it. ui is unsigned. For what possible values of ui would
    > (0x0ul <= ui) or, more simply, (ui >= 0), be false?


    Well I understood the fact that no value of ui can make this condition
    false.
    But when I replaced 0ul with 0u I didn't get the warning.When I
    retained ul and compiled it on a 32bit machine ( ul == 32bits ) I
    didn't get the warning either. I was just trying to understand the
    significance of ul being 64 bits to trigger this warning.I should've
    mentioned this in the original post.
     
    sridhar, Nov 2, 2004
    #4
  5. (sridhar) writes:
    > Keith Thompson <> wrote in message
    > news:<>...
    >> (sridhar) writes:
    >> > #include <stdio.h>
    >> > int main(){
    >> > unsigned int ui = 0;
    >> > if(0x0ul <= ui){
    >> > printf("less eq\n");
    >> > }
    >> > }
    >> >
    >> > On my system unsigned long is 64 bits and unsigned int is 32.The
    >> > compiler gives a warning
    >> >
    >> > warning: comparison is always true due to limited range of data type

    >>
    >> You're not getting the warning because you're comparing ui to an
    >> unsigned long; you're getting the warning because you're comparing ui
    >> to zero.
    >>
    >> Think about it. ui is unsigned. For what possible values of ui would
    >> (0x0ul <= ui) or, more simply, (ui >= 0), be false?

    >
    > Well I understood the fact that no value of ui can make this condition
    > false.
    > But when I replaced 0ul with 0u I didn't get the warning.When I
    > retained ul and compiled it on a 32bit machine ( ul == 32bits ) I
    > didn't get the warning either. I was just trying to understand the
    > significance of ul being 64 bits to trigger this warning.I should've
    > mentioned this in the original post.


    Unless I'm missing something obvious, it's probably just an odd quirk
    of the compiler you're using. I can't think of any reason why
    changing 0x0ul to 0x0u should inhibit the warning. (You used the same
    command-line options in both cases, right?)

    --
    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, Nov 2, 2004
    #5
  6. In article <>,
    (sridhar) wrote:

    > Keith Thompson <> wrote in message
    > news:<>...
    > > (sridhar) writes:
    > > > #include <stdio.h>
    > > > int main(){
    > > > unsigned int ui = 0;
    > > > if(0x0ul <= ui){
    > > > printf("less eq\n");
    > > > }
    > > > }
    > > >
    > > > On my system unsigned long is 64 bits and unsigned int is 32.The
    > > > compiler gives a warning
    > > >
    > > > warning: comparison is always true due to limited range of data type

    > >
    > > You're not getting the warning because you're comparing ui to an
    > > unsigned long; you're getting the warning because you're comparing ui
    > > to zero.
    > >
    > > Think about it. ui is unsigned. For what possible values of ui would
    > > (0x0ul <= ui) or, more simply, (ui >= 0), be false?

    >
    > Well I understood the fact that no value of ui can make this condition
    > false.
    > But when I replaced 0ul with 0u I didn't get the warning.When I
    > retained ul and compiled it on a 32bit machine ( ul == 32bits ) I
    > didn't get the warning either. I was just trying to understand the
    > significance of ul being 64 bits to trigger this warning.I should've
    > mentioned this in the original post.


    My explanation is a bit complicated: Obviously a comparison like "0x0ul
    <= ui" is useless. It might indicate a bug in your program, and that is
    when compilers are supposed to give warnings.

    A typical example would be

    unsigned int ui;
    for (ui = 10; ui >= 0; --ui) f (ui);

    This is an infinite loop, which might come as a surprise to the
    programmer who wrote it, and it definitely should get a warning. On the
    other hand, such useless comparisons are very often not a bug,
    especially when the operands are constants. For example:

    #define VERSION 3 /* Could be 1, 2, 3 or 4 */

    if (VERSION == 1) {
    ...
    } else if (VERSION == 2) {
    ...
    } else if (VERSION == 3) {
    ...
    } else if (VERSION == 4) {
    ...
    }

    After macro substitution, all the comparisons are "useless" but there is
    no bug here. So it is quite likely that a compiler doesn't give warnings
    when only constants are involved.

    It could be that because of the mix of unsigned int and unsigned long in
    your code the compiler doesn't recognize that only constants are
    involved; it might not recognize "(unsigned long) ui" as a constant
    anymore, and therefore you get a warning.

    There are no exact rules for warnings, every compiler does its best to
    give warnings on suspicious code and give no warnings on code that works
    as intended, and getting this right is difficult.
     
    Christian Bau, Nov 2, 2004
    #6
  7. (sridhar) wrote in message news:<>...
    > Keith Thompson <> wrote in message news:<>...
    > > (sridhar) writes:
    > > > #include <stdio.h>
    > > > int main(){
    > > > unsigned int ui = 0;
    > > > if(0x0ul <= ui){
    > > > printf("less eq\n");
    > > > }
    > > > }
    > > >
    > > > On my system unsigned long is 64 bits and unsigned int is 32.The
    > > > compiler gives a warning
    > > >
    > > > warning: comparison is always true due to limited range of data type

    > >
    > > You're not getting the warning because you're comparing ui to an
    > > unsigned long; you're getting the warning because you're comparing ui
    > > to zero.
    > >
    > > Think about it. ui is unsigned. For what possible values of ui would
    > > (0x0ul <= ui) or, more simply, (ui >= 0), be false?

    >
    > Well I understood the fact that no value of ui can make this condition
    > false.
    > But when I replaced 0ul with 0u I didn't get the warning.When I
    > retained ul and compiled it on a 32bit machine ( ul == 32bits ) I
    > didn't get the warning either. I was just trying to understand the
    > significance of ul being 64 bits to trigger this warning.I should've
    > mentioned this in the original post.


    There is no significance in this case. It's a 'quality of implementation'
    issue. It appears that when the compiler is "thinking about" the two
    operands having a different size, it notices that the comparison is
    always true and warns you about it. When they're the same size, it
    doesn't notice (or doesn't choose to warn you).
     
    J. J. Farrell, Nov 3, 2004
    #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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    750
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,248
    Peter Shaggy Haywood
    Sep 20, 2005
  3. luke
    Replies:
    9
    Views:
    383
    Peter Shaggy Haywood
    Mar 12, 2006
  4. pereges

    Promoting unsigned long int to long int

    pereges, Jun 30, 2008, in forum: C Programming
    Replies:
    112
    Views:
    2,178
    David Thompson
    Jul 28, 2008
  5. pozz
    Replies:
    12
    Views:
    793
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page