What will be the output of the following C code?#include<stdio.h>int main(){ int a=0, b=1, c=2; *((a+1 == 1) ? &b : &a) = a ? b : c; printf("%d, %d, %d\n", a, b, c); return 0;} Ops: A. 0,2,2 B. 2,2,2 C. 0,1,2 D. 1,1,2
Question
What will be the output of the following C code?#include<stdio.h>int main(){ int a=0, b=1, c=2; *((a+1 == 1) ? &b : &a) = a ? b : c; printf("%d, %d, %d\n", a, b, c); return 0;} Ops: A. 0,2,2 B. 2,2,2 C. 0,1,2 D. 1,1,2
Solution
The output of the given C code will be 0,2,2.
Here's the step by step explanation:
-
In the expression
*((a+1 == 1) ? &b : &a) = a ? b : c;, first(a+1 == 1)is evaluated. Sinceais 0,a+1equals 1, so(a+1 == 1)is true. -
Because the condition is true,
&b(the address ofb) is selected by the ternary operator? :. -
On the right side of the
=operator,a ? b : cis evaluated. Sinceais 0 (which is considered false in C),cis selected by the ternary operator. -
Therefore,
c(which is 2) is assigned to the location pointed by&b, which meansbis now 2. -
Finally,
printf("%d, %d, %d\n", a, b, c);prints the values ofa,b, andc, which are 0, 2, and 2 respectively.
So, the correct answer is A. 0,2,2.
Similar Questions
what will be the output of the c program?
What will be the output of the following code?
What will be the output of the following C++ code?
What will be the output of the following program?
What will be the output of the following C code?#include <stdio.h>int main() { int i; for (i = 0; i < 5; i++); printf("%d\n", i); return 0;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.