0

Can anyone explain why it's printing "panni" instead of "-23 23"

int main() { unsigned int i = 23; signed char a = -23; if(i>a) printf("%d %d",a,i); else if(i<a) printf("panni"); return 0; }

16th May 2020, 6:48 AM
Marana Coder
Marana Coder - avatar
3 odpowiedzi
+ 2
You are actually comparing signed and unsigned values. Because 'i' is at first place, C convert 'a' to an unsigned value... As you know, -23 > 23 (if you don't understand why, search for the binary representation of a negative value), so it is false. If you're working in C, try to cast 'i' to a signed value.
16th May 2020, 7:15 AM
Théophile
Théophile - avatar
+ 1
I don't know if that's the issue but I'd use if / else instead of if / else if Maybe you're missing an else statement? Then again I'm not familiar with signed / unsigned comparison
16th May 2020, 7:06 AM
HNNX 🐿
HNNX 🐿 - avatar
0
Théophile can you elaborate what value it (a) will be converted ?? And how the conversion happens? If there any study link please share. It'll be more helpful
16th May 2020, 7:25 AM
Marana Coder
Marana Coder - avatar