By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Parentheses Ineffectual

P: n/a

If you wish to override the natural operator precedence and associativity
in C, you can use parentheses:

5 * (4 - (2 + 7))

However, I was surprised to discover (by testing) yesterday that some
operators are unaffected by parentheses. I expected the following program
to print:

Func2() called, returns 0.
Func1() called, returns 0.
However, with my own compiler, it prints:

Func1() called, returns 0.
The parentheses used in "main" have no effect on the && operator.
#include <stdio.h>

int Func1(void)
{
puts("Func1 called, returns 0.");

return 0;
}

int Func2(void)
{
puts("Func2 called, returns 0.");

return 0;
}

int Func3(void)
{
puts("Func3 called, returns 0.");

return 0;
}

int main(void)
{
Func1() && ( Func2() && Func3() );
}
Any thoughts on this?

--

Frederick Gotham
Jul 9 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Jul 9 '06 #2

P: n/a

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:15*******************@news.indigo.ie...
>
If you wish to override the natural operator precedence and associativity
in C, you can use parentheses:

5 * (4 - (2 + 7))

However, I was surprised to discover (by testing) yesterday that some
operators are unaffected by parentheses. I expected the following program
to print:

Func2() called, returns 0.
Func1() called, returns 0.
However, with my own compiler, it prints:

Func1() called, returns 0.
The parentheses used in "main" have no effect on the && operator.
#include <stdio.h>

int Func1(void)
{
puts("Func1 called, returns 0.");

return 0;
}

int Func2(void)
{
puts("Func2 called, returns 0.");

return 0;
}

int Func3(void)
{
puts("Func3 called, returns 0.");

return 0;
}

int main(void)
{
Func1() && ( Func2() && Func3() );
}
Any thoughts on this?
The compiler must do this. The left most expression of an AND or OR is
executed first. If the result of the expression can be determined, the
second half may not be executed.
Thus you can say

if( x 0 && y/x 100)

in the knowledge that the expression y/x will never cause a divide by zero.

--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Jul 9 '06 #3

P: n/a
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:

Func2() called, returns 0.
Func1() called, returns 0.
However, with my own compiler, it prints:

Func1() called, returns 0.

The parentheses used in "main" have no effect on the && operator.

...( Func1,2 &3 deleted)

int main(void)
{
Func1() && ( Func2() && Func3() );
}

Any thoughts on this?
Your expectations are wrong:

-------------------------------------
ISO/IEC 9899:1999(E)

6.5.13 Logical AND operator
....
Semantics
....
4 Unlike the bitwise binary & operator, the && operator guarantees
left-to-right evaluation; there is a sequence point after the
evaluation of the first operand. If the first operand compares equal
to 0, the second operand is not evaluated.
---------------------------------------

Had you used the '&' operator instead, you would see the output of all
three functions in *any* order. All three operands would have been
evaluated. The order of evaluation of the operands is not the order of
evaluation of the operators. It is only the later the one that can be
influenced by the use of parenthesis.
Jul 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.