Reputation: 4990
I am trying to run this simple code
int a=0;
cout<<a<<a++;
but the output is not what I expected
10
I would expect "00" and a=1, why is the answer different?
Upvotes: 0
Views: 493
Reputation: 21
It is simple... As per my knowledge in C++ order of execution of any statement starts from right... in cout<
Upvotes: 0
Reputation: 154007
And what do you expect? Or more correctly, you're wrong to expect anything: you're modifying a variable, and accessing it for reasons other than determining the value to write, with no intervening sequence point, so the code has undefined behavior. It might output "10", it might output "01", or it might output "42", or even crash.
Upvotes: 3
Reputation: 2156
The C++ standard does not specify an order of execution for subexpressions
Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified...
Upvotes: 2