anirudh sn wrote: |
---|
Result=1. |
No, the result is
undefined. The flaw in your logic is in step 4:
step 4:
--a*b(original value)/b(original value): 5/5
|
That 2nd 'b' may or may not be the original value. It may be the incremented value. This behavior is not defined by the C++ standard and you must never do this in actual code.
EDIT: To clarify further...
For purposes of this example, let's simplify the expression to isolate the parts we're interested in:
Operator precedence rules dictate that ++ will be evaluated before /. This is guaranteed. What
isn't dictated is the order of the expressions around the / in relationship to each other.
That is, there are two smaller expressions here:
b++
and
b
. Both must be fully evaluated before the division operator is evaluated.
But in which order are they evaluated? It isn't specified... so it's undefined.
The compiler might evaluate
b
first, resulting in c=2. Or it might evaluate the
b++
first, resulting in c=0.
Or it might evaluate part of b++ first, then b, then finish evaluating the incrementing part of b++, resulting in c=1
Or it might do something completely different, resulting in any number of possible outcomes.