Slazer
Slazer

Reputation: 4990

Order of outputting with cout output stream

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

Answers (3)

SNK
SNK

Reputation: 21

It is simple... As per my knowledge in C++ order of execution of any statement starts from right... in cout<

Upvotes: 0

James Kanze
James Kanze

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

DuncanACoulter
DuncanACoulter

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

Related Questions