Tricky question in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Tricky question in C

int a=1; short int b=2; float c=3; if(sizeof((a==2)?c:b)==sizeof(float)) {printf("a");} else if(sizeof((a==2)?c:b)==sizeof(short int)) {printf("b");} I expected the output of the above code to be b. Turns out, the output is a. How is it possible?

6th Feb 2019, 3:18 PM
Ajeya Bhat
Ajeya Bhat - avatar
3 Answers
+ 1
int a=1; short int b=2; float c=3; # if (sizeof((a==2)? c:b)==sizeof(float)) - compare a==2, since its not same, then condition comes to b it is 2 - sizeof(2), 2 is integer, so the result is 4 - then we compare it to sizeof(float), so 4==4, then the result is 1 - if(1) is same like if(true), as long as the number in the parenthesis is not 0 or false, then execute statement in the if condition - So finally the result is "a" Note: the point is careful to the parenthesis, because the result is depending on this
24th Feb 2019, 5:14 AM
Okta Alfiansyah
Okta Alfiansyah - avatar
0
a=1:b=2:c=3 ((1==2)?3:2)==4 ->false ((1==2)?3:2)==2 ->true so the output will be b
8th Feb 2019, 11:49 AM
Oviya arumugam
Oviya arumugam - avatar