% more price.c
int price = 10;
int cost = price++ + ++price;
% gcc price.c
% gcc -O price.c
I reported this error once before, back in the 2.95 days, I think.
price is modified more than once without an intervening sequence point, so the
code is invalid.
The answer should be 22. When I compile using gcc/g++, the
answer is 21. When I compile using gcc/g++ -O, the answer
is correct. I reported this bug once before.
I don't know what invalid code means. The code is syntactically
correct, but one gets different semantics depending on whether
the compiler does or does not optimize. It seems reasonable
to expect the same output regardless of the level of optimization.
The code has undefined behavior, which means that printing 21,
printing 22, printing 42, crashing, refusing to compile, and
formatting your hard disk would all be correct results. Please read
the documentation on -Wsequence-point and a good C book.
*** Bug 12569 has been marked as a duplicate of this bug. ***
Reopen these bugs...
...mark as duplicate of PR 11751.
*** This bug has been marked as a duplicate of 11751 ***