+9  A: 

The expression

j = j++ * ++j

...is undefined. There is no way to know what the order of these operations are. It could result in anything, or make your CPU burst into flames. It will differ from compiler to compiler, and even within a particular compiler depending on optimisation levels or versions.

detly
A: 

The operator precedence should be post-increment then pre-increment then multiply although I bet this comes down to one of those compiler-dependent / undefined things.

This precedence is based on the table at Wikipedia which is pretty common across the web which states that post-increment has precedence 2, pre-increment has precedence 3, and multiply has precedence 5.

My intuitive look is:

j = j++ * ++j (j is 2)
j = 2 * ++j (j is now 3)
j = 2 * 4 (j is now 4)
j = 8

Which is obviously wrong.

The correct answer is obviously that this is undefined.

Graphain
+12  A: 

int j=2; j= j++ * ++j; printf("%d", j);

j=j++*++j invokes Undefined Behavior as you are trying to change the value of j more than once between two sequence points.

Precedence only affects which tokens are considered to be the operands of each operator, but it does not affect in any way the order of evaluation.

Prasoon Saurav
In C++0x the term "sequence point" is being replaced by the term "an operation A being sequenced before an operation B, or being un-sequenced"(see http://blogs.msdn.com/b/vcblog/archive/2007/06/04/update-on-the-c-0x-language-standard.aspx, Sequence Points Revised)
Patrick
Well that just rolls off the tongue, doesn't it :P
detly
@Patric although the question mentions it's using a C++ compiler, it's tagged C, so changes to the wording of C++ standards might not be entirely relevant.
Pete Kirkham
@Downvoter: Please explain why you downvoted.
Prasoon Saurav
A: 

I can guess that passed through the following way of computation:

  • pre-increment j by one
  • multiply j by itself
  • post-increment j by one

The exact order of these computations is undefined by the standard and may be compiler-specific.

Vanya
The order of evaluation of arguments associated to `*` and `=` operators is `Unspecified`. Don't confuse among the terms `Undefined`, `Implementation Defined` and `Unspecified`.
Prasoon Saurav
Definitions of implementation-defined, undefined, and unspecified at http://stackoverflow.com/questions/2046952/limit-the-confusion-caused-by-undefined-behavior/2047172#2047172.
Roger Pate