On 12/2/10 6:24 PM, Eric Smith wrote:
A[i++] = i++;
I can't quote chapter and verse, but I'm fairly sure the standard will
say that the result of that assignment is undefined.
In pre-standard C, it is also undefined, but for a different reason.
Many programmers would say "I would never write code like that", but
I've seen statements like that creep into code as a result of
cut-and-paste or global-search-and-replace operations that weren't
carefully checked. It's also quite possible when writing complex
expressions to come up with things like that which are not necessarily
obvious to casual inspection. It is good that many compilers will
detect these things and generate warnings. When I have the power to
do so, I generally treat warnings from the compiler as fatal errors
requiring a fix, and not acceptable in released code.
One "common" scenario occurs when using macros:
in some header file:
#define FOO(x) x++
in some source file
A[i++] = FOO(i);
so someone casually looking at the code won't realize that the macro has
a side effect.