Interview Questions

If I am not using the value of the expression?

C Interview Questions and Answers


(Continued from previous question...)

If I am not using the value of the expression?

Q: If I'm not using the value of the expression, should I use ++i or i++ to increment a variable?

Since the two forms differ only in the value yielded, they are entirely equivalent when only their side effect is needed. (However, the prefix form is preferred in C++.) Some people will tell you that in the old days one form was preferred over the other because it utilized a PDP-11 autoincrement addressing mode, but those people are confused. An autoincrement addressing mode can only help if a pointer variable is being incremented and indirected upon, as in
register char c, *cp;
c = *cp++;

(Continued on next question...)

Other Interview Questions