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;
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. Join The Discussion
Comments ( 1 )
Related Questions on C Fundamentals
What is the primary purpose of a function prototype in C?
A. Declare a variable
B. Declare a function
C. Define a function
D. Assign a value
What is the correct syntax for declaring a variable in C?
A. int variable_name;
B. variable_name = 5;
C. variable_name int;
D. int = variable_name;
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