I have the following C code:
unsigned int a;
unsigned char b, c;
void test(void) {
if (a < b)
return;
if (a < (b ? b : c))
return;
}
When I compile it (with Microsoft cl, from MS SDK 7, -W3 warning level), the second comparison emits a warning: C4018, signed/unsigned mismatch. The first comparison emits no warning.
I’ve checked MS docs on the conditional operator and they says that if both operands are of same type, the result will be of the same type, so it should work as the first comparison. Am I missing something?
UPD: tested with gcc -Wall -Wextra -pedantic and got no warnings whatsoever.
This is probably due to the arithmetic conversion rules: First, any integer type of conversion rank less than
int(egunsigned char) will promote tointorunsigned int.Whether the result will be
intorunsigned intdoes not (directly) depend on the signedness of the original type, but its range:intis used even for unsigned types as long as all values can be represented, which is the case forunsigned charon mainstream architectures.Second, as both operands end up with the same conversion rank, but one is unsigned, the other operand will be converted to an unsigned type as well.
Semantically, your expressions read
and
The compiler is apparently smart enough to notice that the first case cannot cause problems, but fails for the second one.
Steve Jessop’s comment nicely explains how this could happen: