Quote:
Originally Posted by weaknessforcats
So this works just because the lvalue is different?
-
i+= b -= c*= d /= 1;
-
i = b = c = d = 5;
-
K&R says the order of evaluation of operands is unspecified.
So should I be coding:
-
i += (b -= (c *= (d /= )));
-
i = (b = ( c = (d = 5)));
-
I've never seen code like that.
I know that:
is indeterminate if f() and g() alter a variable that they both depend on.
Help me out here.
The definition for undefined behaviour (sic) is quite simple: when a mutable lvalue
(e.g. a variable) is changed more than once before a sequence point is reached
(a semicolon) the behaviour (result) is undefined.
Your example above is fine because your altering different mutable lvalues only once.
Parentheses don't help: they alter the precedence or associativity only, e.g.a-b-c is
different from a-(b-c). The order in which the operands a, b, and c are evaluated is
unspecified indeed.
The original example where the mutable lvalue 'i' is altered twice causes undefined
behaviour according to the spec.
I don't have the Standard text available on this laptop (I left the other one at the office),
but remind me if you want me to email the text (it's the latest draft of the C99 Standard).
kind regards,
Jos