Under the rules for expressions (section 5, paragraph 4 of the ISO C++
standard):
"Except where noted, the order of evaluation of operands of
individual operators and subexpressions of individual
expressions, and the order in which side effects take place,
is unspecified. Between the previous and next sequence
point a scalar object shall have its stored value modified at
most once by the evaluation of an expression. Furthermore,
the prior value shall be accessed only to determine the value
to be stored. The requirements of this paragraph shall be met
for each allowable ordering of the subexpressions of a full
expression; otherwise the behavior is undefined.
[Example:
i = v[i++]; // the behavior is unspecified
i = 7, i++, i++; // i becomes 9
i = ++i + 1; // the behavior is unspecified
i = i + 1; // the value of i is incremented
-end example]"
In particular notice the final words "otherwise the behavior is
undefined" along with "a scalar object shall have its stored value
modified at most once by the evaluation of an expression". I would also
be suspicious of "the prior value shall be accessed only to determine
the value to be stored". Your expressions
v=v+(m++)+(++m);
are more extreme versions of
the form of the third example given in the standard (i = ++i + 1;).
lets take one more extreme condition
x=3
x-=--x-x--;
What will be the output?
Lets see !
I think in this particular case the code could be behaving as follows:
int x = 3;
--x; // x = 2
int tmp = x - x; // tmp = 0
x--; // x = 1
x -= tmp; // x -= 0, thus x = 1
or maybe:
int x = 3;
--x; // x = 2
int tmp = x - x; // tmp = 0
x -= tmp; // x -= 0, thus x = 2
x--; // x = 1
ok now have other form of the above expression
x= x- --x- x--;
int x = 3;
int tmp1 = x - x; // tmp1 = 0
int tmp2 = tmp1 - x; // tmp2 = -3
x = tmp2; // x = -3
x--; // x = -4
x--; // x = -5
Of course these are probably not the only ways to break down your expressions
and obtain the results you are seeing.
In general do not make expressions too complex - they are harder to get
right and harder to debug (you cannot see the intermediate temporary
results - and yes the compiler will use temporary objects if
necessary). And sometimes compilers can get confused with certain forms
of complex expressions!
Yeah these kind of statements are not against any law or rule of C language but
it does not confirm you
what you are thinking about the final answer will you get or not as we have
seen above.
The different compilers will handle the situations in different ways.
Also the more complex the statement would be
more will be the no of ways to handle it
and so the more will be results.
Therefore to have a unique and same answer each time try to break up your
complex statements into simple ones.
Of course we are programmers and we should know what we want with the program
to perform.
Now after this I think one more query will develop in your mind about simple
statements.
That is whether to follow left to right direction or right to left direction
for the execution of statement.
The answer is that it varies with the operator you are using in the statement.
Operators follow a strict precedence, which defines the evaluation
order of expressions containing these operators. Operators associate
with either the expression on their left or the expression on their
right; this is called "associativity."
For example.
the associativity of prefix operators like ++or -- is
Right to Left
while
the associativity of postfix operators like ++or-- is
Left to right
for more information on
associativity
follow the link:
http://msdn.microsoft.com/en-us/library/126fe14k(VS.80).aspx
I hope now i made every thing clear and understandable.
[Non-text portions of this message have been removed]