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.
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.
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.
int variables.
– tdammers
Nov 09 '12 at 11:18
int is signed by default.
– Useless
Nov 09 '12 at 11:29
"%d" would crash if you pass a char, but apparently, it implicit-casts to an appropriate integer type.
– tdammers
Nov 09 '12 at 11:47
@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: