Why the output is like this ? Please explain | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
16th Oct 2019, 7:50 PM
Beginner
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
16th Oct 2019, 8:16 PM
sneeze
sneeze - avatar
+ 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.
16th Oct 2019, 8:08 PM
Shadow
Shadow - avatar