In article <15*******************@news.indigo.ie>,
Frederick Gotham <fg*******@SPAM.comwrote:
>However, I was surprised to discover (by testing) yesterday that some
operators are unaffected by parentheses. I expected the following program
to print:
>int main(void)
{
Func1() && ( Func2() && Func3() );
}
>Any thoughts on this?
The && operator is defined such that the left operand is executed
and its value tested, and the right operand is ignored if the
left operand it false. This allows constructs such as
x != 5 && (1/(x-5) < y)
to be well defined.
Always remember that parentheses do not mean that the indicated
section must be executed first out of all of the terms in the
expression: the parenthesis indicate groupings and need not be
paid attention to until the value of the grouping is required for
the connecting operator.
For instance, in your example, 5 * (4 - (2 + 7))
this does not mean that (4 - (2 + 7)) must be evaluated first:
the two operands of * are at equal priority and the compiler may
evaluate either one of them first. It is free to evaluate the 5 first
and only then proceed to (4 - (2 + 7)). Inside the outer (),
the compiler can execute the operands of the - in either order,
possibly executing the 4 first before proceeding on to the (2+7).
You can experiment with this by using, for example,
int p(int x) { printf("p argument was %d\n", x); x }
p(5) * (p(4) - (p(2) + p(7)))
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell