post incerment isro 2014

The following three 'C' language statements is equivalent to which single statement?

  1. z = x + y + 2;
  2. z = (x++) + (++y);
  3. z = (x++) + (y++);
  4. z = (x++) + (++y) + 1;
Parth Sharma @parthsharmau
23 Apr 2017 08:25 pm

Correct answer should be 2 

As y is incremented first (++y pre increment )and then the sum of x and y is assigned to z and after that assignment x is incremented (x++ post increment)

Sumit Verma @sumitkgp
24 Apr 2017 12:21 am

These types of question are compiler dependent but you can answer according to the general logic.
In the 2nd option z = (x++) + (++y); first of all the right hand side expression will be evaluated.
Here you can see that y has pre increment operator, so value of y will increase 1 and will be added into the current value of x. Result of the expression will be stored into z and then post increment operator will increase the value of x by 1.
So it is similar to the sequence of instructions given in 3 lines.

Parth Sharma @parthsharmau
24 Apr 2017 06:16 am

Actually there is no option like none of these or compiler dependent that is why we can go for the concepts of sequence point logic .