Search code examples
clong-integerunsigned-integer

why is a negative long value greater than a positive unsigned long value?


Example code:

long num1 = -1;
unsigned long num2 = 1;

if (num1 > num2)
    printf("Num1 is greater\n");
else
    printf("Num2 is greater\n");

Why does this evaluate num1 > num2 as true instead of it being false?


Solution

  • This is the result of an integer promotion rule described in C99 standard, section 6.3.1.8:

    if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

    This is exactly what happens in your example: long and unsigned long have the same rank, so the signed long is converted to unsigned long.

    Since -1 cannot be represented as an unsigned, the following rules come into play:

    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. 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.

    The result is, therefore, 1- + ULONG_MAX-1, which equals ULONG_MAX. That is why the result of converting -1 to unsigned is greater than 1.