1. Home
  2. Computer Science MCQs
  3. C Programming
  4. After int x = (1, 2, 3); what value does x hold in C?

Added 30 Nov -0001

After int x = (1, 2, 3); what value does x hold in C?

  1. 6, because the values are summed
  2. A compile-time error, since commas are invalid
  3. 3, because the comma yields the last value Correct answer
  4. 1, because the first value is assigned

Correct answer

C. 3, because the comma yields the last value

14 views

MCQSEXAM

Explanation

The comma operator evaluates its left operand, discards the result, and yields the right operand, so (1, 2, 3) evaluates left to right and produces 3. Its low precedence is why the parentheses are required; writing int x = 1, 2, 3; is parsed as separate declarators and does not compile. The operator appears legitimately in for-loop updates such as i++, j--.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs