I've a question about order of evaluations in expressions that have && and
|| operators in them.
Several others have given correct answers (and one person a wildly
wrong one

). C does have a rule that is relatively easy to
remember here, though, and I think no one has yet described it.
That rule is: evaluation order is specified *only* by what C calls
"sequence points".
A compiler's job is to take some source-language description of
"what should happen" and re-arrange it, remove some of it, add to
it, and so on, so that some kind of computer hardware can make it
run as fast as possible. This slicing-and-dicing of your code is
allowed as long as the result is still what you originally asked
for. C compilers get to do this "cheaply" by piling up as much
action as they can between these "sequence points".
A sequence point is where the action stops. Stopping the action
makes "everything so far" finish, before the computer can go on to
"everything afterward". In other words, it establishes a particular
order of operations. (The compiler can still "slice and dice" your
code about, even across these sequence points, but *only* if it
can prove that you will not be able to tell that it has done so.
For instance, if you print output, that output must somehow magically
erase if it should not have been printed after all, and so on.)
This makes sequence points absolutely crucial, because as a C
programmer, you *need* to make sure things happen in specific
sequences. If you want to add 1 to i, or change p to point to
p->next, you *have* to have that happen before you use the new
values in i and p.
So the general answer to your question is that order of evaluation
is defined only if you have some of these "sequence points". Given
a sequence point, and some operation A that happens "before" and
some operation B that happens "after" that sequence point, every
C compiler must do A first, then do B (or, if it secretly slices
and dices things so that they happen simultaneously or in the other
order, it has to arrange an illusion, so that A *seems* to happen
before B, no matter what really goes on inside the machine).
in this expression: a = f() + g(); it isn't guaranteed that f() will be
called first...
Correct. Formally, the "+" and "=" operators have no sequence
points at all. (The function calls do have "embedded" sequence
points, but they are no help because we have no idea which function
gets called first.)
The question is: will the evalution go left-to-right, no matter
what -- even if the || operator is before the && operator? e,g,
In an expression like a = (z>x) || (x<0) && (z-y>9);
is it guaranteed that z>x will be checked first?
Here you are in luck. The C standard (either C89 or C99, there is
no real change here) says that the logical operators have sequence
points, and that given an expression with a logical operator, the
left hand side is evaluated first, then you get the sequence point.
If the result of the operator is now known, the right hand side is
not evaluated at all. (This "skip the other half if we know the
answer" technique is usually called "short-circuit evaluation".
Some other languages provide short-circuit evaluation too. For
instance, in Ada, one can write "if a and then b" to mean "test a;
then, only if the answer is not yet known, test b". Leaving out
the "then" -- "if a and b" -- allows the compiler to test both.
Paradoxically, it is actually sometimes faster to test both than
to skip one of the tests. These kinds of details are machine-dependent
though.)
Note that:
a = (z>x) || (x<0) && (z-y>9);
parses as:
a = (z > x) || ((x < 0) && ((z - y) > 9));
That is, the "&&" binds more tightly than the "||", and the "-"
in "z - y" binds more tightly than the ">" in "... > 9". So
what we have is a logical-OR-expression whose right hand side
is a logical-AND-expression. If you were to write instead:
a = ((z > x) || (x < 0)) && ((z - y) > 9);
you would have a logical-AND-expression whose left hand side
is a logical-OR-expression. Interestingly, while this changes
the overall result (and thus sometimes skips a different set
of evaluations), this has no effect on the evaluation *order*
if all sub-expressions are evaluated. (Exercise: why? Remember
that the rule is: "evaluate left side, then have a nice nap at a
sequence point, then evaluate right side only if necessary".)