Output of C programs | Set 55 (Ternary Operators)

Predict the output of below programs
Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    int x, a = 0;
    x = sizeof(a++) ? printf("Geeks for Geeks\n") : 0;
    printf("Value of x:%d\n", x);
    printf("Value of a:%d", a);
    return 0;
}

chevron_right


Output:

Geeks for Geeks
Value of x:16
Value of a:0

Explanation: sizeof is a compile-time operator, so at the time of compilation sizeof and its operand get replaced by the result value. The operand is not evaluated (except when it is a variable length array) at all; only the type of the result matters. In sizeof operator, a++ will not evaluated. So it will remain same i.e. value of a will be 0.
printf returns the number of width. Geeks for Geeks is of 16 width. This will return 16.So x is now 16.



Question 2

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    int x;
    x = 5 > 8 ? 10 : 1 != 2 < 5 ? 20 : 30;
    printf("Value of x:%d", x);
    return 0;
}

chevron_right


Output:

Value of x:30

Explanation:

exp1?exp2:exp3          
5 > 8 ? 10: 1!= 2<5 ? 20:30

Output of exp1 is false, so exp3 (1 != 2 <5 ? 20 : 30) will be evaluated. In exp3, this is also form of ternary operator.
1 != 2< 5 ? 20 : 30 (exp1 ? exp2 : exp3)
Now, exp1 will be evaluated. According to operator precedence, 2<5 will be evaluated first (will give output 1). Now, exp1 is like 1!=1 (condition is false). So, exp3 will be evaluated. Therefore, final output is 30.


Question 3

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    int x;
    x = 5 < 8 ? 1 != 2 < 5 == 0 ? 10 : 20 : 30;
    printf("Value of x:%d", x);
    return 0;
}

chevron_right


Output:

Value of x:10

Explanation :

exp1?exp2:exp3
5 < 8? 1!=2<5 == 0 ? 10:20:30

exp1 is true, so exp2 will be evaluated. exp2 is also in form of ternary operator.
1!= 2<5 == 0? 10 : 20 (exp1?exp2:exp3)
exp1(1 != 2 < 5 ==0)is evaluated. In exp1, according to operator precedence, 2<5 will be evaluated first(condition is true). Now exp1 is 1!=1==0. Again, according to operator precedence, 1==0 is evaluated(condition is false). Now exp1 is 1!=0(condition is true). Now, exp2 will be evaluated. Therefore, final output is 10.


Question 4

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
#include <stdio.h>
int main()
{
    int x;
    x = 2 > 5 != 1 ? 5 < 8 && 8 > 2 ? !5 ? 10 : 20 : 30 : 40;
    printf("Value of x:%d", x);
    return 0;
}

chevron_right


Output:

Value of x:20

Explanation:

exp1?exp2:exp3
2 > 5 != 1 ? 5 2 ? !5 ? 10:20:30:40

exp1 will be evaluated always. According to operator precedence, 2>5 will be evaluated first(condition is false). Now, exp1 is 0!=1.Condition is true.So, exp2 will be evaluated, this is also in ternary operator form.


52 ? !5 ? 10:20:30 
(exp1?exp2:exp3)

exp1 will be evaluated. According to operator precedence, 52 will be evaluated(condition is true). Now, exp1 is 1&&1(condition is true). So exp2 will be evaluated, it is also in form of ternary operator.

!5 ? 10:20 (exp1?exp2:exp3)

exp1 will be evaluated, value of !5 is zero, so exp3 will be evaluated.Final output will be 20.


Question 5

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    int x;
    x = 2 > 5 ? 1 != 2 > 5 ? 10 : 20 : 5 < 8 ? 2 != 2 > 5 ?
    !5 ? 30 : !1 != 1 ? 40 : 50 : 60 : 70;
    printf("Value of x:%d", x);
    return 0;
}

chevron_right


Output:

Value of x:40

Explanation

exp1?exp2:exp3
2>5   ?   1!=2>5?10:20   :  55?!5?30:!1!=1?40:50:60:70;

exp1 will be evaluated always.2>5(condition is false), so exp3 will be evaluated, which is also in form of ternary operator.

55?!5?30:!1!=1?40:50:60 : 70  (exp1?exp2:exp3)

exp1 will be evaluated, 5<8(condition is true), so exp2 will be evaluated, which is also in form of ternary operator.

2!=2>5  ?  !5?30:!1!=1?40:50  :  60 (exp1?exp2:exp3)

exp1 will be evaluated.According to operator precedence, 2>5 will be evaluated first(condition is false). So, exp1 is now 2!=0(condition is true). Now, exp2 will be evaluated, which is in form of ternary operator.

!5  ?  30  :  !1!=1?40:50  (exp1?exp2:exp3)

exp1 will be evaluated, value of !5 is zero. So, exp3 will be evaluated, which is in form of ternary operator.

!1!=1  ?  40  :  50  (exp1?exp2:exp3)

exp1 will be evaluated.According to operator precedence, !1 will be evaluated first ie. 0.Now, exp1 is 0!=1(condition is true), exp2 will be evaluated. Therefore final output is 40.

This article is contributed by Vinay Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.