Bug 12552

Summary: Problem with increment operator gcc/g++
Product: gcc Reporter: devnull
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: gcc-bugs, s0094247
Priority: P2    
Version: 3.3   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description devnull 2003-10-09 16:27:46 UTC
% more price.c
#include <stdio.h>

int main()
{
  int price = 10;

  int cost = price++ + ++price;
  printf("%d\n", cost);
  return 0;
}

% gcc price.c
% ./a.out
21
% gcc -O price.c
% ./a.out
22

I reported this error once before, back in the 2.95 days, I think.
Comment 1 Falk Hueffner 2003-10-09 16:30:50 UTC
price is modified more than once without an intervening sequence point, so the
code is invalid.
Comment 2 devnull 2003-10-09 16:33:53 UTC
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.

Thanks!!

Mark
Comment 3 devnull 2003-10-09 16:40:35 UTC
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.
Comment 4 Falk Hueffner 2003-10-09 16:43:26 UTC
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.
Comment 5 Andreas Schwab 2003-10-10 15:20:32 UTC
*** Bug 12569 has been marked as a duplicate of this bug. ***
Comment 6 Wolfgang Bangerth 2004-08-05 14:57:13 UTC
Reopen these bugs... 
Comment 7 Wolfgang Bangerth 2004-08-05 15:02:00 UTC
...mark as duplicate of PR 11751. 

*** This bug has been marked as a duplicate of 11751 ***