+ 2
Why the output is like this ? Please explain
https://code.sololearn.com/c1Oi0w3Dm8ac/?ref=app https://code.sololearn.com/c1Oi0w3Dm8ac/?ref=app
2 Answers
+ 1
a>b is a boolean comparision
if you represent this boolean with a %d, you are going to make a number of it.
0 is false
1 is true
%d is the first number
%% print % , repeating character prints one
%d for the second number
The first comparision is true due to implicit conversion
The second comparision is false because -2 is smaller than 5
https://code.sololearn.com/c1Oi0w3Dm8ac
quote :
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.
https://softwareengineering.stackexchange.com/questions/175253/why-does-an-unsigned-int-compared-with-a-signed-character-turn-out-with-an-unexp
+ 2
Because in order to compare the elements, they need to have the same type, so a is implicitely converted to unsigned int, the type of b. Since unsigned integers can not store negative values, a loops all the way around to four-million-somewhat (use printf( "%u", a ); to get the actual value) and is therefore quite a bit bigger than b is. However, when comparing a and c, both share a common type, so they are compared "as is", where c is of course bigger.