Increment and decrement operators in postfix and prefix mode

I am using Microsoft Visual Studios 2010, and when In input the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
int a = 5, b = 5,
    c = 3, d = 7; 
  
   cout << a-- << ' ';
   cout << a   << ' ';
   cout << a-- << ' ' << a-- << ' ';
   cout << a-- << ' ' << a << endl;
    
   cout << ++b << ' ';
   cout << b   << ' ';
   cout << ++b << ' ' << ++b << ' ';
   cout << ++b << ' ' << b << endl;
   
   a = c++ * d--;
   cout << a << "  " << c << ' ' << d << endl; 
            
   return 0;
}


My numbers look like:
5 4 3 4 2 1
6 6 8 8 9 9
21 4 6

However, when I input the code this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
 int a = 5, b = 5,
     c = 3, d = 7; 
   
   cout << a-- << ' ';
   cout << a   << ' ';
   cout << a-- << ' ';
   cout << a-- << ' ';
   cout << a-- << ' ';
   cout << a << endl;
    
   cout << ++b << ' ';
   cout << b   << ' ';
   cout << ++b << ' ';
   cout << ++b << ' ';
   cout << ++b << ' ';
   cout << b << endl;
    
   a = c++ * d--;
   cout << a << "  " << c << ' ' << d << endl; 
            
   return 0;
 }


I get what I would expect:
5 4 4 3 2 1
6 6 7 8 9 9
21 4 6

Can somebody tell me in a bit of detail why the outputs are different?

Thanks!
-jumpinmp
The first program contains errors:
test.cc: In function 'int main()':
test.cc:11:36: warning: operation on 'a' may be undefined [-Wsequence-point]
test.cc:12:35: warning: operation on 'a' may be undefined [-Wsequence-point]
test.cc:16:36: warning: operation on 'b' may be undefined [-Wsequence-point]
test.cc:17:35: warning: operation on 'b' may be undefined [-Wsequence-point]

Both modifying the same scalar variable twice without synchronization (lines 11 and 16) and modifying and reading the same scalar variable without synchronization (lines 12 and 17) are explicitly undefined in C++ (or in C, for that matter). Executing an undefined program can give any results whatsoever.

You can read more about these rules at http://en.cppreference.com/w/cpp/language/eval_order
Last edited on
OK, thanks, Cubbi. Visual studios doesn't report any errors (that I know how to detect, anyhow). How were you able to find those errors?

This is for a C++ lab out of a 7th edition textbook that I'm using to take a college course... One would hope those errors would have been caught by now, lol.

Anyhow, thanks again!

Thanks,
-jumpinmp
How were you able to find those errors?

I could see them, but I ran your program though gcc just in case (and to save on typing)

7th edition textbook that I'm using to take a college course... One would hope those errors would have been caught by now

It's the unfortunately common problem with C++ educational materials - they aren't usually written by people familiar with the language.
Thanks again.

Thanks,
-jumpinmp
Topic archived. No new replies allowed.