Why does the following code output y>x when clearly 1>-1?
unsigned x=1;
signed char y=-1;
if(x>y){
printf("x>y");
}
else {
printf("y>x");
}
Please explain this result.
1
Implicit type conversion is biting you. Because x
is unsigned, y
is cast to unsigned as well; because -1 doesn’t fit in an unsigned char, it overflows and becomes 255 (the bit-wise unsigned char equivalent of -1), which obviously is larger than -1.
13
@tdammers answer is correct, let me expand a bit.
The binary representation for negative values assumes the highest bit has value of 1
. E.g., all negative 8-bit values look like 1xxx xxxx
.
8-bit value for -1
is 1111 1111
.
Hence, the very same binary 1111 1111
for unsigned is 255
. So, the same binary value can be interpreted two different ways, depending if it is signed or not.
In your case, as @tdammers noticed, your -1
is stored into signed variable, but then implicitly interpreted as unsigned. Of course, 1 < 255
, hence the result.
P.S. You should always check compiler warnings. All modern compilers would raise a warning on this matter.
This is a case where hopefully you are getting a compiler warning about the mixing of a signed and unsigned value. It may be even more specific where it talks about an unsigned lvalue and a signed rvalue.
This example underscores the hazards of C and to a lesser extent, C++ (which tends to be a little more strict about type checking, and which offers multiple kinds of casts). If you want to write good quality C code several things can help:
- As a basic precaution, use compiler options to generate the strictest warnings possible.
- Very specifically, avoid mixing of signed and unsigned values in comparisons and most calculations. (signed = signed * unsigned OK, signed = signed + unsigned OK, most other mixing, bad).
- Use printf(), trace(), or inspection of variables in a debugger to better understand where things go wrong. If your program included a statement like printf(“x=%d, y=%d/n”, x, y); you would see that things went bad as -1 was assigned into y.
- Study, study, study… C is a demanding language and you should make yourself as expert as possible on the semantics and side-effects of signed and unsigned comparisons and calculations. You also need a very high degree of attention to detail about the range of input values to equations, the possible range of equation results, and whether constants can actually fit into the variables to which they are assigned.
- When appropriate, use defensive programming techniques like asserts to expose programming problems during development.
2