+ 1

C - Why is output 0?

int x = 1?0:2?3:4; printf("%d",x);

15th Jan 2020, 7:59 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
5 Answers
+ 4
Because the first conditional is true then it does not evaluate the second conditional.
15th Jan 2020, 8:45 PM
NicolĂĄs VĂĄzquez
NicolĂĄs VĂĄzquez - avatar
+ 2
It's conditional statement so that if the condition is true then it stops and won't execute the remaining code.So the output is 0 not 4
16th Jan 2020, 4:42 AM
Dheeraj
Dheeraj - avatar
+ 1
Paolo De Nictolis any conditional operator, and, or operator which decide the code execution by evaluation of any part true or false have an property or follow an property which is known as "Short Circuit behavior" this behavior occurs when any part RHS or LHS evaluated to true it won't check for remaining part Conditional expressions group right-to-left. The first expression is contextually converted to bool . It is evaluated and if it is true, and if that part is true other part evaluation is halted have an read here at wiki page https://en.m.wikipedia.org/wiki/Short-circuit_evaluation and enjoy 🍎 🍎
15th Jan 2020, 9:34 PM
DishaAhuja
DishaAhuja - avatar
0
Conditional ? True : False; 1 ? = True => x = 0
15th Jan 2020, 8:32 PM
NicolĂĄs VĂĄzquez
NicolĂĄs VĂĄzquez - avatar
0
I understand. But why second conditional isn't executed, so that x "remains" 0, instead of being 4?
15th Jan 2020, 8:34 PM
Paolo De Nictolis
Paolo De Nictolis - avatar