cout precedence doubt

Hello.

I was using cout, and something happened that I wasn't expecting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int returnI();

int i = 0;

int main(void) {

	cout << returnI() << returnI() << returnI();

	return 0;
}

int returnI() {
	i++;
	return i;
}


The output that I was expecting is "123", since the precedence of the << operator is from left to right, but the actual output is "321". Why is this happening?

Thanks in advance.
The order of the function calls is undefined. You are only guaranteed the results are evaluated in a specific order.

BTW, just because you offloaded it into a global variable and a function doesn't mean that you can ignore sequence points. Google around that for more.

Hope this helps.
Topic archived. No new replies allowed.