ProgrammingVery hardGATE CS · Product company OA

With int i = 1, x; int a[5] = {0}; which of these statements have undefined behaviour? (i) i = i++ + 1; (ii) a[i] = i++; (iii) i = (i, i + 1); (iv) x = (i++, i);

  1. A. (i) and (ii) only
  2. B. (i) only
  3. C. All four
  4. D. (i), (ii) and (iv) only

Correct answer: A. (i) and (ii) only

(i) modifies i twice with no sequence point between (the ++ and the assignment). (ii) reads i to pick the element and modifies it in i++, unsequenced. The comma operator in (iii) and (iv) is a sequence point: its left side finishes before its right side is evaluated, so both are well defined.

More C Programming questions

Exams that ask this topic