Here is what is going on here:

n++ evaluates the expression first and add 1 to it. Therefore

n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =

11

p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.

In short,

++n increment the n first

n++ evaluate the expression first

Hope it explained your confusion.

Dev

"Karlo Basic" <ka*****@email.si> wrote in message

news:bo*************@ID-212926.news.uni-berlin.de...

Greetings!

I'm wondering how do the expressions in the following piece of code

evaluate and why:

#include <iostream>

using namespace std;

int main()

{

int n = 5, p;

n = n + n++;

cout << "n = " << n << endl;

n = 5;

p = n + n++;

cout << "p = " << p << endl;

return 0;

}

I compiled it both with g++ and bcc32 and the results are identical:

n = 11

p = 10

The first result seems obvious. But I don't know what's going on with

the evaluation of the p variable. I am really interested on how it

works. Thank you in advance,

Karlo.