Examveda
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 = 2;

C. a = 2, c = 2;

D. a = 1, c = 2;

Answer: Option C

Solution(By Examveda Team)

In the given C statement, there is a ternary operator being used. The ternary operator has the syntax: condition ? expression1 : expression2;
If the condition evaluates to true, expression1 is executed; otherwise, expression2 is executed. In this case, the condition is c. If c is true (non-zero), a will be assigned the value 0; otherwise, a will be assigned the value 2. Simultaneously, c will be assigned the value 2.
Given that c = 1, which is true (non-zero), a will be assigned 0 and c will be assigned 2.
Therefore, the final values will be a = 0 and c = 2. So, Option C: a = 2, c = 2; is correct.

This Question Belongs to C Program >> C Fundamentals

Join The Discussion

Comments ( 1 )

  1. Aaniketh
    Aaniketh :
    3 months 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