Bug 12552 - Problem with increment operator gcc/g++
Summary: Problem with increment operator gcc/g++
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.3
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 12569 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-10-09 16:27 UTC by devnull
Modified: 2005-07-23 22:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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 ***