+ 2
Why this code output is 0?can anyone elaborate this
int exp= 1?0:2?3:4; printf("%d",exp);
4 Réponses
+ 2
What i was thinking is since 0 is answer for 1 value 0?3:4 will be taken..so since 0 is false 4 will be printed thats what i thought off
+ 2
Thanks mate appreciate for that reason and suggestion
+ 1
Ternary operator
exp1 ? exp2 : exp3
If exp1 is is nonzero then exp2 is evaluated and it becomes the value of the whole expression otherwise exp3.
1 ? 0 : (2 ? 3 : 4);
1 is nonzero so 0 is the result of the expression.
int exp = 0;
0
Be careful when nesting ternary operators. Add parenthesis for readability and it also prevents from logic/syntax errors.