Output of C programs | Set 55 (Ternary Operators)
Last Updated :
19 Sep, 2017
Predict the output of below programs
Question 1
C
#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;
}
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
C
#include <stdio.h>
int main()
{
int x;
x = 5 > 8 ? 10 : 1 != 2 < 5 ? 20 : 30;
printf("Value of x:%d", x);
return 0;
}
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
C
#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;
}
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
C
#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;
}
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
C
#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;
}
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.
Similar Reads
Output of C programs | Set 55 (Shift Operators) Prerequisite: Shift operators Q.1 What Is The Output Of this program? C #include <stdio.h> int main() { unsigned int i = 0x80; printf("%d ", i << 1); return 0; } Option a) 0 b) 256 c) 100 d) 80 ans :- b Explanation :- We know that 0x is hexa-decimal representation of number so
3 min read
Output of C programs | Set 58 (operators) Prerequisite : Operators in C Q.1 What is the output of this program? CPP #include <iostream> using namespace std; int main() { printf("value is = %d", (10 ++)); return 0; } Options a) 10 b) 11 c) compile time error d) run time error ans: c Explanation : lvalue required as increment
3 min read
Output of C programs | Set 53 (Operators) Prerequisite: Operators in C 1. What will be the output of the following? CPP #include <stdio.h> int main(void) { int a; int b = 1; int x[5] = { 1, 2, 3, 4, 5 }; a = 5 * 4 + x[--b] - (9 / b); printf("%d", a); return 0; } Options: 1. 12 2. Abnormal Termination 3. 21 4. No Output The a
2 min read
Output of C programs | Set 49 (Operators) Prerequisite : Operators in C 1. What will be the output of the following program? CPP #include <stdio.h> int main(void) { int i = 40 >> 5 << 3 >> 2 << 1; printf("%d", i); return 0; } Options: 1. 4 2. 0 3. 40 4. 1 Answer : (1) Explanation: The answer is option
3 min read
Output of C programs | Set 55 1. What will be the output of the following program? C #include <stdio.h> int main() { int a = 03489; printf("%d", a); return (0); } Options: 1. 1865 2. runtime error 3. Syntax error 4. 0 The answer is option(3). Explanation: Any integral value prefix with 0 acts as octal number but
3 min read
Output of C Programs | Set 5 Predict the output of below programs Question 1 c int main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } return 0; } Output: Can't be predictedExplanation: The condition in while loop is 1 so at first shot it looks infinite loop. Then there are break and con
5 min read