Examveda

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;

B. a = 2, c = 1;

C. a = 2, c = 2;

D. a = 1, c = 2;

Answer: Option A

Solution (By Examveda Team)

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

This Question Belongs to C Program >> C Fundamentals

Join The Discussion

Comments (2)

  1. Dhruv Prajapati
    Dhruv Prajapati:
    3 months ago

    Step-by-step explanation:
    This uses the ternary operator:

    c
    Copy
    Edit
    condition ? expr_if_true : expr_if_false;
    So we evaluate:

    c is currently 1 → which is true (non-zero in C).

    Therefore, the true part is executed:

    c
    Copy
    Edit
    a = 0
    and this assignment expression returns the value 0.

    So the full statement becomes:

    c
    Copy
    Edit
    c = a = 0; // a becomes 0, and c is assigned that same value
    ✅ Final values:
    a = 0

    c = 0

    ✅ Answer:
    a = 0, c = 0

  2. Aaniketh
    Aaniketh :
    2 years ago

    It should be because
    c = (c) ? a = 0 : 2; // a = 0 will become c's value as (c) is true since it has a non zero value

    next step will be
    c = a = 0; // in this one, c is also defined as 0

Related Questions on C Fundamentals