Interview question, this?

Saw this on a message board I have started frequenting:

hi

j=1;cout<<j++<<endl<<j<<endl<<++j ;
the following code segment gives 233 as against expected 123. Can somebody explain why.

bye

Firstly, ghastly code! I wouldn’t like to hire someone who writes that!

Anyway, the C++ standard states that you cannot modify an object more than once between sequence points – the behaviour is unspecified and compiler dependent.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s