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



> From: James
> On Wed, 28 Apr 1999, Glynn Clements wrote:
> 
> # > In gcc,
> # > 
> # > if i = 2;
> # > then j = i++ + ++i;
> # > 
> # >   what is the value of j.
> # 
> #     i++ == 2
> #     ++i == 3
> # =>  j   == 5
> 
> no. it's not. I typed this in:
> 
> main() { int i = 2; printf ("%d", i++ + ++i);}
> and when i ran it i got 6 printed...
> 

Reply via email to