See this code snippet
int main()
{
unsigned int a = 1000;
int b = -1;
if (a>b) printf("A is BIG! %d\n", a-b);
else printf("a is SMALL! %d\n", a-b);
return 0;
}
This gives the output: a is SMALL: 1001
I don’t understand what’s happening here. How does the > operator work here? Why is “a” smaller than “b”? If it is indeed smaller, why do i get a positive number (1001) as the difference?
Binary operations between different integral types are performed within a “common” type defined by so called usual arithmetic conversions (see the language specification, 6.3.1.8). In your case the “common” type is
unsigned int. This means thatintoperand (yourb) will get converted tounsigned intbefore the comparison, as well as for the purpose of performing subtraction.When
-1is converted tounsigned intthe result is the maximal possibleunsigned intvalue (same asUINT_MAX). Needless to say, it is going to be greater than your unsigned1000value, meaning thata > bis indeed false andais indeed small compared to(unsigned) b. Theifin your code should resolve toelsebranch, which is what you observed in your experiment.The same conversion rules apply to subtraction. Your
a-bis really interpreted asa - (unsigned) band the result has typeunsigned int. Such value cannot be printed with%dformat specifier, since%donly works with signed values. Your attempt to print it with%dresults in undefined behavior, so the value that you see printed (even though it has a logical deterministic explanation in practice) is completely meaningless from the point of view of C language.Edit: Actually, I could be wrong about the undefined behavior part. According to C language specification, the common part of the range of the corresponding signed and unsigned integer type shall have identical representation (implying, according to the footnote 31, “interchangeability as arguments to functions”). So, the result of
a - bexpression is unsigned1001as described above, and unless I’m missing something, it is legal to print this specific unsigned value with%dspecifier, since it falls within the positive range ofint. Printing(unsigned) INT_MAX + 1with%dwould be undefined, but1001uis fine.