Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsigned long 0 < -1?

Tags:

c

I don't get this one!

#include <stdio.h>

int main()
{
    unsigned short t1 = 0, t2 = 0;

    if( t1 < t2-1 )
        printf(" t1 < t2-1\n");

    unsigned long s1 = 0, s2 = 0;

    if( s1 < s2-1 )
        printf(" s1 < s2-1\n");
}

this results in:

 s1 < s2-1

Either both should fail or both not. I tried this with gcc 4 & 4.2

like image 915
nairboon Avatar asked Sep 12 '09 16:09

nairboon


1 Answers

Iam not sure but I suspect that the expression t2-1 automatically widened into an int value. I do not have the c standard here what the exact conversion rules are, but I believe types smaller than int are automatically widened.

like image 115
codymanix Avatar answered Sep 20 '22 05:09

codymanix