Q56
What will be the final values of a and c in the following C statement? (Initial values: a = 2, c = 1) c = (c) ? a = 0 : 2;
A.
a = 0, c = 0;
AnswerB.
a = 2, c = 1;
C.
a = 2, c = 2;
D.
a = 1, c = 2;
Answer: Option A
Solution
Answer: Option A
Solution:
Initial values: a = 2, c = 1;The statement is:
c = (c) ? a = 0 : 2;This is a ternary conditional expression. The condition is (c), which means if c is non-zero, the expression before the colon is executed.
Since c = 1 (non-zero), the true part is executed: a = 0
Then the value of the true expression (a = 0) is assigned to c.
So now:
a becomes 0
c becomes 0 (since the result of a = 0 is 0)
Final values: a = 0, c = 0