I hate it when teachers teach undefined behavior. :angry: The value of j will depend on the compiler you are using because the language does not define the behavior of i.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
It's the order that the sums and increments are executed. In each expresion you listed there are different addition operations to be done and depending on what order you do them effects the result. i++/++i is shorthand for i = i + 1; If you place the ++ before the i (prefix) this is generally accepted as increment i *before* evaluating the expression, putting ++ after the i (postfix) means evaluate the expression first and increment i *afterwards*. Also placing things in parentheses generally means evaluate this part of the expression first, but I imagine they are only in this example for clarity (there's only so many +'s the human eye can take!)
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
1. j= (i++) + (i++);
Compiles to machine instructions in this order:
j = 10 + 10
i = 10 + 1
i = 11 + 1
2. and 3.
i = 10 + 1
J = 11 + 11
I = 11 + 1
4.
i = 10 + 1
i = 11 + 1
j = 12 + 12
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
But like everyone else points out it's kind of nonsense, because 1. why would you ever need to do that anyway? Obfuscation? other than that I can't think of anything. and 2. it's contrary to the standard.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
But like everyone else points out it's kind of nonsense, because 1. why would you ever need to do that anyway? Obfuscation? other than that I can't think of anything. and 2. it's contrary to the standard.
and 3. he has an idot for a teacher.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343