1

Why compiler ignores increment operation? Version of gcc is 3.3. IDE: Eclipde

Program code:

// File t.cc
#include <stdio.h> 
int main (void)
{
  int x = 1;
  x++; // ? 
  printf ("x=%d\n", x);  
  return 0;
}

Running the program:

$ g++ -ansi t.cc
x=1
Dima Kozyr
  • 3,183
  • 7
  • 28
  • 61
  • 2
    [Works for me](http://ideone.com/l1jnyj). Are you sure that's the code you're compiling? – Mike Seymour Feb 20 '14 at 19:17
  • Did you run with "-ansi"? – Dima Kozyr Feb 20 '14 at 19:19
  • 2
    Yes, with the same result. – Mike Seymour Feb 20 '14 at 19:20
  • 1
    @op so without `-ansi` you get the correct result? – Luchian Grigore Feb 20 '14 at 19:23
  • Check [here](http://ideone.com/HoaHQV) a copy paste of your code, how is that different from what you get? – Nikos Athanasiou Feb 20 '14 at 19:24
  • @dima what is ur IDE ? – Roma-MT Feb 20 '14 at 19:26
  • @Pompei2 Version of gcc is 3.3 – Dima Kozyr Feb 20 '14 at 19:29
  • @if u replace `x++; with x+=1;` does it work ? – Roma-MT Feb 20 '14 at 19:31
  • What is the rationale for using the `-ansi` flag? Additionally, why use `printf()` instead of `cout` in a C++ program? Finally, the gcc you are using is 'ancient'. – pauluss86 Feb 20 '14 at 19:32
  • 2
    @dima, your problem is neither your code nor the compiler; it's most probably Eclipse. – pauluss86 Feb 20 '14 at 19:33
  • @dima try to clean your project , and then build it again. (in Eclipse IDE) it helps to solve problems from that kind. – Roma-MT Feb 20 '14 at 19:34
  • 1
    [http://stackoverflow.com/questions/2518428/eclipse-how-do-i-refresh-an-entire-workspace-f5-doesnt-do-it][1] your eclipse project needs to be refresh. [1]: http://stackoverflow.com/questions/2518428/eclipse-how-do-i-refresh-an-entire-workspace-f5-doesnt-do-it – willll Feb 20 '14 at 19:36
  • 1
    There are a couple of problems with this question. If you assume the compiler is at fault, posting the compiler version (and possibly the IDE you're using if it's relevant) is useful. Secondly, there's nothing wrong with the code sample. So one would naturally conclude that either your compiler installation is broken, or that whatever you're omitting from the question is at fault. **ECLIPSE IS NOT A COMPILER!** –  Feb 20 '14 at 19:38
  • 1
    To comment on the question-edit: the compiler is not ignoring anything. I suspect that (1) Eclipse is invoking `g++` by passing it some cached entry, (2) Eclipse is invoking `g++` with the correct code but executes a stale binary, (3) Both. Have you tried wiping your 'workspace' and installing 500 unrelated plugins? You should do the Eclipse dance until she is satisfied ;) – pauluss86 Feb 20 '14 at 19:44
  • Have you tried the command line version of `g++` instead of Eclipse? – Thomas Matthews Feb 20 '14 at 20:45
  • @Thomas Matthews Yes, it's working. I think the problem is in Eclipse – Dima Kozyr Feb 20 '14 at 20:48
  • 1
    @dima: the main problem *is* Eclipse.. – pauluss86 Feb 20 '14 at 21:21

2 Answers2

10

Your compiler is broken. A fix for this would be to get a new one.

Luchian Grigore
  • 236,802
  • 53
  • 428
  • 594
3

Your code returns x value as 2. Re-compile & execute again.

enter image description here

Yuva Raj
  • 3,780
  • 1
  • 17
  • 30
  • 1
    would suggests to clean then rebuild and run :) – Roma-MT Feb 20 '14 at 19:27
  • 1
    @Yuva, it's tagged as C++ and compiled by invoking `g++`. This, combined with the `.cc` extension suggests he is just mixing in C which shouldn't be a problem. – pauluss86 Feb 20 '14 at 19:35