I know that the order of evaluation and precedence of operators in C are independent. But I get confused when there are multiple operators that mandates the order of evaluation.
For example:
a = b && c || d;
is parsed as:
a = ((b && c) || d);
How does the compiler evaluate this?
Does it evaluate (b && c), (if necessary) evaluate d, then evaluate the assignment operator which goes right to left?
Or does it evaluate the assignment operator first, then the && operator and then (if necessary) evaluate d?
In most cases in C, order of evaluation is unspecified.
In this case, we have logical operators, and they do have sequence points:
bis fully evaluated before the start of evaluatingc, for instance.The assignment operator does not have a sequence point, so
amay be computed before or after the value being assigned to it.That's why it's unsafe to modify a variable that's used on both sides of such an operator (e.g.
*++i = *iis undefined).