Interview Questions

Why doesn't this code:

C Interview Questions and Answers


(Continued from previous question...)

Why doesn't this code:

Q: Why doesn't this code:
a[i] = i++;
work?

A: The subexpression i++ causes a side effect--it modifies i's value--which leads to undefined behavior since i is also referenced elsewhere in the same expression. There is no way of knowing whether the reference will happen before or after the side effect--in fact, neither obvious interpretation might hold . (Note that although the language in K&R suggests that the behavior of this expression is unspecified, the C Standard makes the stronger statement that it is undefined-

(Continued on next question...)

Other Interview Questions