If n has 3, then the statement a[++n]=n++;

2015

If n has 3, then the statement a[++n]=n++;

  1. A.

    assigns 3 to a[5]

  2. B.

    assigns 4 to a[5]

  3. C.

    assigns 4 to a[4]

  4. D.

    what is assigned is compiler dependent

Attempted by 1048 students.

Show answer & explanation

Correct answer: D

When n = 3, the statement a[++n] = n++ involves modifying the variable n twice within a single expression — once by ++n (pre-increment) and once by n++ (post-increment) — without any sequence points in between. The C/C++ standard states that modifying a variable more than once between sequence points results in undefined behavior. This means the compiler is free to evaluate the operands in any order, and different compilers may produce different results. Therefore, what is assigned is entirely compiler dependent.

Explore the full course: Isro