Reputation: 131
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?
Upvotes: 2
Views: 283
Reputation: 1
You are comparing two variables of different data types, i.e. long & unsigned long.
In this case, during comparison of two variables, compiler is implicitly typecasting the variable num1 to unsigned long as two variables of different data types can't be compared.
So, num1 is being converted to 65534 [65535 - 1].
Thus, you are getting Num1 is greater
Upvotes: 0
Reputation: 223739
When using a long
and unsigned long
together in an expression, the ‘longvalue is converted to
unsigned long. This results in the value -1 becoming the largest
unsigned long` value, which is greater than 1.
Upvotes: 0
Reputation: 726559
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
.
Upvotes: 6