After int x = (1, 2, 3); what value does x hold in C?
Correct answer
C. 3, because the comma yields the last value
14 views
C. 3, because the comma yields the last value
14 views
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--.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.