I have an exam at uni from C and I was looking through the exams from the previous years and I stumbled over this problem:
What is the value of d after executing the following sequence?
int a=36, b=20, c=30, d;
d = c?(a? a: c):(b? c: b);
There was another exercise like this, but in those parenthesis there were other expressions:
d = (a>b) ? ((a>c)? a: c): ((b>c)? b: c);
I put the 2 codes in ChatGPT and it told me that they are called ternary operators.
I understand that in the second example we are comparing a with b, a with c, b with c, then we are giving d a value based on the comparisons. But in the first example, there are no comparisons, only variables. Moreover, the test will be on paper, so I won't be able to run the code on a computer. How do I read the syntax of the first example, what does it mean? Am I still comparing the 3 variables, or is it something different?
I ran the codes on CodeBlocks and on VS for both exercises, with the same values (a = 36, b = 20, c = 30), and they both gave me the same answer:
d = (a>b) ? ((a>c)? a: c): ((b>c)? b: c); //d = 36
and
d = c?(a? a: c):(b? c: b); //d was still 36.
I don't understand how did I get that answer from the second exercise.
CodePudding user response:
This is the conditional operator; it selects its second or third operand based on its first operand, which is a condition. It is a ternary operator; it has three operands. (But so does the function call f(a, b), with operands f, a, and b.) Do not use ChatGPT for authoritative information.
C 2018 6.5.15 4 specifies the conditional operator:
The first operand is evaluated… The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated), converted to the type described below.
Thus, in c?(a? a: c):(b? c: b), c is evaluated, and the conditional operation proceeds:
- If
cis not zero,(a? a: c)is evaluated. This operation proceeds:- If
ais not zero,ais evaluated. - If
ais zero,cis evaluated.
- If
- If
cis zero,(b? c: b)is evaluated.- If
bis not zero,cis evaluated. - If
bis zero,bis evaluated.
- If
Evaluation of an object identifier (a, b, or c) simply produces its value. Since c is not zero, (a? a: c) is selected. Since a is not zero, a is selected. a is 36.
CodePudding user response:
The first ternary statement:
d = c?(a? a: c):(b? c: b);
is equivalent to this series of if-else:
if (c) { /* c is non-zero */
if (a) /* a is non-zero */
d = a;
else
d = c;
} else {
if (b) /* b is non-zero */
d = c;
else
d = b;
}
The second one:
d = (a>b) ? ((a>c)? a: c): ((b>c)? b: c);
is equivalent to this series of if-else:
if (a > b) {
if (a > c)
d = a;
else
d = c;
} else {
if (b > c)
d = b;
else
d = c;
}
Some notes:
if (a)is equivalent toif (a != 0).if (!a)is equivalent toif (a == 0.- The
ifstatement considers any non-zero value to be true, and zero to be false. The block is only entered if the conditional expression evaluates to a non-zero value.
