Parentheses Ineffectual

  • Thread starter Frederick Gotham
  • Start date
F

Frederick Gotham

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?
 
W

Walter Roberson

Frederick Gotham said:
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)))
 
M

Malcolm

Frederick Gotham said:
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.
 
R

Roberto Waltman

Frederick Gotham said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top