+ 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?
3 ответов
+ 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
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