postincrement behaviour (differences between dmd and gdc)

Nicolai Waniek no.spam at thank.you
Wed Jan 24 12:31:44 PST 2007


Hello again,

I have a huge arguing going on with a friend of mine, which behaviour of
x++ is the right one. This has in fact to do with D, so read on :)

This is our example application:

int x = 5;
x = x++;
x = x++;
print(x);


I argue: the result printed is 7.
He argues: the result printed is 5.

the "++" operator is defined as: "returns the value and then increments
it by 1". so I guess, the execution should be as follows:


int x = 5;
-> x "gains" the value 5.


x = x++;
-> first, (x = x) is executed. afterwards, x is incremented by 1. as x
and x are the same variables, x now has the value 6.

x = x++;
-> the same as above, so x is now 7.


Allright, now let's look what the compilers do:

C/C++ compiled with GCC:	7
java compiled with SUN's:	5
D compiled with DMD:		5
D compiled with GDC:		7

Allright, so please explain this behaviour to me?

Best regards,
Nicolai Waniek




More information about the Digitalmars-d mailing list