> Very interesting problem, and a more complete
> answer would be j = 6, i = 4, and this is why:
> 
> i = 2;
> j = i++ + ++i;
> 
> Obviously, the j line is more interesting, so
> we'll talk about sequence of operations.  The
> preincrement operator is done before everything
> else, so "++i" is done, giving i the value of
> 3, even before the addition, so you end up
> with "j = 3++ + 3" which equals 6.  After the
> line is completed, i is incremented, causing
> i to be 3++ = 4.
> 
> 
> ~Patrick

On my compiler it seems to do this:
 i = 2;
 j = i++ + ++i;
It does the i = 2 just fine.  So at the start of the j line i = 2.
Now, it does i + ++i, and *then* it increments i the second time.  So it
becomes (2 + ++i)++;  Actually I just tried it with a couple different
compilers. g++ and gcc give me 5 for j while CC and cc both give me 7 for
j. I assume that the GNU compilers are incrementing the i++ after then
assignment is done, while the CC compilers are incrementing i++ before the
addition begins.

  --Joe

Reply via email to