Cisco Systems Interview Question
Software Engineer / DevelopersActually, the conversion rules are
a) Convert to the larger type.
b) In case of equal size prefer usigned over signed.
You can verify this by changing the declaration of int b to int64_t b. The output would now say Negative.
If we check
if(a+b == -9)
printf("Hello");
in this case hello is printed. Can anyone explain?
coz at binary level -9 and (unsigned)a+b are same had it been a+b>-9 then it will give some other result since then -9 is treated as signed while a+b as unsigned
That is because signed value is converted into and unsigned, as we know -ve values in c are saved in 2's complement format, and when 2's complement of a negative is considered as unsigned it will become a big +ve number(ex -1 is value when considered as unsigned, its value is 2^32-1(assuming int size is 4 bytes) ). so -19 as unsigned is big positive number, according to your it should be 4294967277.
Answer is Positive, when an unsigned is used in arithmatic with signed the signed is upgraded to unsigned and result is unsigned.
- Prasad May 10, 2010