dimanche 24 mai 2015

Pre increment and Post increment effect [duplicate]

This question already has an answer here:

I just ran the following program.

#include <iostream>
using namespace std;

int main()
{
int x=20, y=35, i=1, j=0;

  x = (++x)+(++y);
  cout << "First step " << x << " " << y << endl;
  y = (x++)+(y++);
  cout << "Second step "  << x << " " << y << endl;
}

The answer on compilation is as follows

 First step  57 36
 Second step 58 93

Though I could understand the first answer as below..

x = (++20) + (++35)
  = 21 + 36 = 57
y = ++35 = 36

I am not able to comprehend the send half.. I expect the below answer.

y =(57++) + (36++)
  = 57 + 36
  = 93 (y++ is pending)
  = 94
x = 57++ (x++ is pending) = 58.

But I get x=58 and y=93.

Can you please point me what am I missing.

Aucun commentaire:

Enregistrer un commentaire