MCS-201 · Unit 2

Data Types, Operators and Expressions

Section 9 of 11

Block 1 · Unit 2Data Types, Operators and Expressions6 min · Section 9 of 11

2.9Conditional and Comma Operators

Covers IGNOU MCS-201 §2.16

Sometimes you need a value that depends on a condition, such as “pass” or “fail”. The conditional operator does this in a single expression. The comma operator, in contrast, lets several expressions run one after another where only one expression is allowed.

The conditional operator

The conditional operator is C’s only operator with three operands, so it is also called the ternary operator.

ExampleMeaning
x = (y < 20) ? 9 : 10;x is 9 if y < 20, otherwise 10
printf("%s\n", marks >= 50 ? "Passed" : "Failed");Prints Passed or Failed
max = (a > b) ? a : b;The larger of a and b

It has very low precedence, just above assignment, so b = (a < f) ? 1 : 0; is the safe way to assign a conditional value.

The comma operator

The comma operator evaluates its left operand, discards its value, then evaluates its right operand; the value of the whole expression is the value of the right operand. Evaluation is strictly left to right.

x = (y = 2, y - 1); first sets y to 2, then evaluates y - 1, so x becomes 1. The parentheses are needed because the comma has the lowest precedence of all operators.

The most common use is in a for loop (Unit 3): for (i = 0, j = n; i < j; i++, j--).

Not every comma is the comma operator: the commas in int a, b; and in printf("%d %d", a, b) are separators.

Key takeaways

  • ?: is the only ternary operator: condition ? value_if_true : value_if_false.
  • Only one branch of ?: is evaluated.
  • Put the whole conditional on the right of =: b = (a < f) ? 1 : 0;.
  • The comma operator evaluates left to right and yields its rightmost value; it has the lowest precedence.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the value of x after x = (7 > 3) ? 100 : 200;?

Show answer

Answer: A. 100

The condition is true, so the second operand, 100, is the result. 1 would be the value of the condition itself.

Question 2 · multiple choice

With int a = 2, b = 9;, what are a and b after int m = (a > b) ? a++ : b--;?

Show answer

Answer: D. a = 2, b = 8

2 > 9 is false, so only b-- runs: m gets 9 and b becomes 8. a++ is never evaluated, so a stays 2.

Question 3 · multiple choice

What is stored in x after x = (5, 10, 15);?

Show answer

Answer: C. 15

The comma operator evaluates left to right and yields the rightmost value, 15. It does not add the values.

Question 4 · multiple choice

Which line compiles correctly in C?

Show answer

Answer: B. max = (a > b) ? a : b;

The conditional expression belongs on the right of a single assignment. The first form is grouped as an assignment to the result of ?:, which C rejects.

Question 5 · multiple choice

In which of these is the comma an operator rather than a separator?

Show answer

Answer: C. x = (a = 1, a + 4);

Inside the parentheses of an expression, the comma sequences a = 1 and a + 4. In declarations, argument lists and enum lists, commas only separate items.

Question 6 · short answer

Write a single statement using the conditional operator that stores the absolute value of an integer n in abs_n, and trace it for n = −7.

Show worked answer

abs_n = (n < 0) ? -n : n;. For n = −7: the condition −7 < 0 is true, so only -n is evaluated, giving 7. Therefore abs_n is 7, and n itself is unchanged.

↑ ↓ move↵ openTab switch type