What is printed by this C code: int i = 0; int r = i++ || i++; printf(“%d %d”, r, i);?
Correct answer A. 1 2
Explanation
|| has a sequence point after its left operand. The first i++ yields 0 (false) and makes i 1, so the right operand must be evaluated: it yields 1 (true) and makes i 2. The whole expression is therefore 1. Short-circuiting would skip the right side only if the left were true, which is why 1 1 is wrong. The sequence point also makes this code well defined.
Answer checked How we write and check questions
Discussion