Very simple question

S

sahteaccount

int i = 1;
cout<< i++ <<" "<< i++ <<" "<< i <<endl;

When compiled under Linux, it gives this output: 2 1 3
When compiled under MS Visual Studio in Debug mode, it gives this
output: 2 1 1
When compiled under MS Visual Studio in Release mode, it gives this
output: 2 1 3

Shouldn't the output be 1 2 3?

Why is this so? What do I do wrong here?

Thanks
 
K

Kai-Uwe Bux

int i = 1;
cout<< i++ <<" "<< i++ <<" "<< i <<endl;

When compiled under Linux, it gives this output: 2 1 3
When compiled under MS Visual Studio in Debug mode, it gives this
output: 2 1 1
When compiled under MS Visual Studio in Release mode, it gives this
output: 2 1 3

Shouldn't the output be 1 2 3?

1 2 3 is a permissible output. And so are all the ones you found.

Why is this so? What do I do wrong here?

You have undefined behavior. Google the archives for "sequence points", and
you will find many answers to the frequently asked question that you
posted.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top