Interview Questions

So if I write ..

C Interview Questions and Answers


(Continued from previous question...)

So if I write ..

Q; So if I write
a[i] = i++;
and I don't care which cell of a[] gets written to, the code is fine, and i gets incremented by one, right?

A; Not necessarily! For one thing, if you don't care which cell of a[] gets written to, why write code which seems to write to a[] at all? More significantly, once an expression or program becomes undefined, all aspects of it become undefined. When an undefined expression has (apparently) two plausible interpretations, do not mislead yourself by imagining that the compiler will choose one or the other. The Standard does not require that a compiler make an obvious choice, and some compilers don't. In this case, not only do we not know whether a[i] or a[i+1] is written to, it is possible that a completely unrelated cell of the array (or any random part of memory) is written to, and it is also not possible to predict what final value i will receive.

(Continued on next question...)

Other Interview Questions