could anyone please explain why when i write this code
    int x = 6;
    cout << x << endl << ++x << endl;
the output is 7 7
instead of
6 7
sorry i'm a beginner in this language , when i moved from VB.Net to C++ i feel it's more complicated ..
could anyone please explain why when i write this code
    int x = 6;
    cout << x << endl << ++x << endl;
the output is 7 7
instead of
6 7
sorry i'm a beginner in this language , when i moved from VB.Net to C++ i feel it's more complicated ..
 
    
    cout << x << endl << ++x << endl;
translates to
cout.operator<<(x).operator<<(endl).operator<<(++x).operator<<(endl);
(where each operator<< call returns a reference to cout for precisely this call chaining), and this is essentially the same as
operator<<(operator<<(operator<<(cout, x), endl), ++x)
(with the last/outermost call discarded for clarity, and the member function call rewritten as an equivalent free function).
Ignoring the endl (which doesn't touch x), the critical part is just a function call of the form
struct C;
C& f(C&, int);
...
f(f(c, x), ++x)
and, like any other function call, the order in which its arguments are evaluated is not defined. That is, the compiler can emit
int a = f(c, x);
int b = ++x;
f(a, b);
or
int b = ++x;
int a = f(c, x);
f(a, b);
There are good reasons for leaving this evaluation order unspecified (not undefined): different platforms have different calling conventions, and it gives the optimiser more room to manoeuvre.
Still, if you care about the order in which they're evaluated, you have to make it explicit.
 
    
    In a c++ expression, evaluation order of sub-expressions is not specified by the standard and is left as implementation-defined. Thus in your expression your expectation that the x part is evaluated before the ++x part is unfounded: the standard makes no such requirement. In your case the ++x sub-expression is evaluated first, resulting in 7 being passed as the parameter value in both cases of the operator<< invocation. It could just as easily have been the other way round, leading to your expected output: both are correct from the standard's point of view.
