2

I'm new to C++ and study the increment and decrement operators. So I tried this example:

    int x = 4;    
    cout << ++x << "    " << x++ << "    " << x++ << endl << endl;
    cout << x << endl;

It returns this weird output on C++ .NET and QtCreator and 5 online C++ compilers:

7    5    4

7

The weird thing is that I expect something like this:

5    5    6

7

Can you explain what happens?

oo_miguel
  • 2,374
  • 18
  • 30
AliOsm
  • 541
  • 2
  • 6
  • 20

3 Answers3

14

Please notice that cout << x++ << ++x; is just another notation for:

operator<<( operator<< (cout, x++), ++x);

The order in which your x++ and ++x statements are evaluated is undefined, so is the effect of your code.

Even if it seems to happens from right to left in your particular examples, you should not rely on that by any means.

Just make another experiment by using multiple statements as:

cout << ++x << " ";
cout << x++ << " ";
cout << x++ << endl;

The solution to your problem is:

Never write code which results in undefined behaviour! :)

oo_miguel
  • 2,374
  • 18
  • 30
  • How can I define the statements order? – AliOsm Oct 30 '15 at 22:36
  • I'm understand I can use multiple statements but I need to know how the compiler execute my example. – AliOsm Oct 30 '15 at 22:42
  • 3
    @AliOsm Learning what the compiler does with unspecified code is a false economy. It may be interesting, but the knowledge is almost worthless because you cannot count on it to remain the same. Your compiler may give one number, your neighbour's another, and your uncle Bob may get a third number. The compiler can literally do whatever it wants. – user4581301 Oct 30 '15 at 23:02
2

The result is undefined for your example. This is because the order of evaluation of x++ or ++x in a line are undefined.

In short if you want predictable behaviour when using operator ++ then the variable it is applied to should only appear once in the expression being evaluated.

wrangler
  • 3,454
  • 1
  • 19
  • 28
  • You mean if the compiler see the ++ operator more than 1 time in the same line don't make priorities and run it only? – AliOsm Oct 30 '15 at 22:41
  • 1
    @AliOsm..it means if the compiler sees ++ operator more than once it is not known in which order it will compute.... – wrangler Oct 30 '15 at 22:43
1

This is what is called undefined beehive, and is dependent on what compiler you use, to better understand this you have to know computer architecture and how compiler works:

cout << x++ << hello << x++ << endl

one compiler can convert this sequence to binary sequence, that would do following

increment x 
print x
print hello
increment x
print x
print endl

while second compiler can do it as following

print x
increment x
print hello
print x
increment x
print endl

third one can do it as following

print x
print hello
print x
print endl
increment x

and forth one can do following

increment x
print x
print hello
print x
print endl

your best bet to sort this issue is:

x++;
cout << x << hello;
x++;
cout << x << endl;
  • I'm use C++ on .NET framework, and I need to know how the compiler execute my example and print that output. – AliOsm Oct 30 '15 at 22:44
  • That is the problem I do not know I think Visual Studio is going backward. Put `endl` onto the stack increment x, put hello onto the stack, increment x put it onto the stack, at the end print it. –  Oct 30 '15 at 22:47