(I can see from Google that this issue confuses a lot of people...)
If you are using GCC, which I think you are (VC++ gives me "1 1" for your program, but GCC "2 1"), then you should probably add the following flags to your compiler build settings (if you don't already have them):
-Wall -Wextra -pedantic
With your little program, GCC gives me the warning
main.cpp:7:36: warning: operation on 'i' may be undefined [-Wsequence-point] |
The "sequence points" that the GCC messages are another part of the picture.
In addition to the indeterminant order of evaluation of function parameters (which cire has already pointed out), you are not allowed to modify a variable more than once without an intervening sequence point (to more or less quote the stock phrase.)
If you haven't read up about sequence points yet, then see:
Sequence point
http://en.wikipedia.org/wiki/Sequence_point
(The article discusses ambiguity, etc.)
And in a similar vein, the answer this tiny program displays
1 2 3 4 5 6 7 8 9 10
|
#include <iostream>
using namespace std;
int main()
{
int j = 1;
int k = j++ + j++ + j++ + j++;
cout << k << "\n";
return 0;
}
| |
is...
(for both GCC and VC++)
Andy
PS The answer in this stackoverflow.com thread might also be of interest as it quotes the appropriate bits of the C++ standard:
Behavior of post increment in cout [duplicate]
http://stackoverflow.com/questions/3986361/behavior-of-post-increment-in-cout