473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Parentheses Ineffectual


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
3 1603
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: seberino | last post by:
Tuples are defined with regards to parentheses ()'s as everyone knows. This causes confusion for 1 item tuples since (5) can be interpreted as a tuple OR as the number 5 in a mathematical...
3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
6
by: KathyB | last post by:
Hi, I'm passing a parameter to a script, which sometimes fails. However, I discovered that the ones failing had parentheses within the text string, e.g., "Step: Press the (0) on the console." ...
6
by: Michael Winter | last post by:
I'll be frank: what is the point? The use, and usefulness, of a set of capturing parentheses in a regular expression is clear. However, isn't an expression such as "/(?:x)/", exactly the same as...
13
by: rick | last post by:
the following line appears in the K&R book in section 5.7: leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; my compiler suggests parentheses "around && within ||" given that the AND...
4
by: Thelma Lubkin | last post by:
I've inherited code with SELECT statements that look like this one: SELECT tblSales.YEAR As , Count(tblSales.UNITS) As FROM INNER JOIN tblSales ON .STFID = tblSales.STFID WHERE...
3
by: Lyn | last post by:
Can anyone explain this for me? A sub procedure can be called with or without parentheses around the arguments. By personal convention, I normally use parentheses. I am in the middle of a...
3
by: Helen Wheels | last post by:
Can we use parentheses in a check constraint in MS-SQL-server DDL? e.g. I'm having a problem with the following statement: ALTER TABLE . ADD CONSTRAINT CHECK (( IS NULL AND IS NULL) OR (...
7
by: vlsidesign | last post by:
I am not sure how to think about parentheses. It seems when they are used alongside characters it indicates a function, like addTwoNums(). But is also is used to enforce which operators and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.