Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Precedence of Logical Operators

Reply
Thread Tools

Precedence of Logical Operators

 
 
August Karlstrom
Guest
Posts: n/a
 
      11-20-2005
Hi,

Can someone explain the reason for the warning from GCC below:

<shell-session>

$ cat test.c
#include <stdbool.h>

bool a, b, c, d;
int x, y, z, u;

int main(void)
{
d = a || b && c;
u = x + y * z;
return 0;
}

$ gcc -Wall test.c
test.c: In function ‘main’:
test.c:8: warning: suggest parentheses around && within ||

</shell-session>

For sake of consistency, why not suggest parentheses around `y * z' as
well? (which would be absurd)


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
 
Reply With Quote
 
 
 
 
Jordan Abel
Guest
Posts: n/a
 
      11-20-2005
On 2005-11-20, August Karlstrom <(E-Mail Removed)> wrote:
> Hi,
>
> Can someone explain the reason for the warning from GCC below:
>
> $ gcc -Wall test.c
> test.c: In function ‘main’:
> test.c:8: warning: suggest parentheses around && within ||
>
> </shell-session>
>
> For sake of consistency, why not suggest parentheses around `y * z' as
> well? (which would be absurd)


Logical operators are easier for people to get confused about because
they are less familiar.
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      11-20-2005
"August Karlstrom" writes:

> Can someone explain the reason for the warning from GCC below:
>
> <shell-session>
>
> $ cat test.c
> #include <stdbool.h>
>
> bool a, b, c, d;
> int x, y, z, u;
>
> int main(void)
> {
> d = a || b && c;
> u = x + y * z;
> return 0;
> }
>
> $ gcc -Wall test.c
> test.c: In function ‘main’:
> test.c:8: warning: suggest parentheses around && within ||
>
> </shell-session>
>
> For sake of consistency, why not suggest parentheses around `y * z' as
> well? (which would be absurd)


"A foolish consistency is the hobgoblin of little minds" - Emerson


 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      11-20-2005
Jordan Abel wrote:
> On 2005-11-20, August Karlstrom <(E-Mail Removed)> wrote:
>
>>Hi,
>>
>>Can someone explain the reason for the warning from GCC below:
>>
>>$ gcc -Wall test.c
>>test.c: In function ‘main’:
>>test.c:8: warning: suggest parentheses around && within ||
>>
>></shell-session>
>>
>>For sake of consistency, why not suggest parentheses around `y * z' as
>>well? (which would be absurd)

>
>
> Logical operators are easier for people to get confused about because
> they are less familiar.


What about

d = a == b && c;

then? GCC issues no warning with the statement above. In Pascal

d := a = b AND c

means

d := a = (b AND c)


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
 
Reply With Quote
 
Sandeep
Guest
Posts: n/a
 
      11-20-2005
August Karlstrom wrote:
> Hi,
>
> Can someone explain the reason for the warning from GCC below:
>
> <shell-session>
>
> $ cat test.c
> #include <stdbool.h>
>
> bool a, b, c, d;
> int x, y, z, u;
>
> int main(void)
> {
> d = a || b && c;
> u = x + y * z;
> return 0;
> }
>
> $ gcc -Wall test.c
> test.c: In function 'main':
> test.c:8: warning: suggest parentheses around && within ||
>
> </shell-session>
>
> For sake of consistency, why not suggest parentheses around `y * z' as
> well? (which would be absurd)


It _may_ be beacuse of the fact that while evaluating an expression
involving logical operator, compilers optimize the code so that even if
one of the condition evaluates to "true/false" (depending on the
complete statement) , the remaining expressions are _not_ evaluated.


Eg:
if( a || b && c && d && e)

here if "a" evaluates to true, the other expression might not be
evaluated. This can sometime lead to unexpected results if parentheses
are not properly provided.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-20-2005
August Karlstrom <(E-Mail Removed)> writes:
> Jordan Abel wrote:
>> On 2005-11-20, August Karlstrom <(E-Mail Removed)> wrote:
>>>Can someone explain the reason for the warning from GCC below:
>>>
>>>$ gcc -Wall test.c
>>>test.c: In function ‘main’:
>>>test.c:8: warning: suggest parentheses around && within ||
>>>
>>></shell-session>
>>>
>>> For sake of consistency, why not suggest parentheses around `y * z'
>>> as well? (which would be absurd)

>> Logical operators are easier for people to get confused about because
>> they are less familiar.

>
> What about
>
> d = a == b && c;
>
> then? GCC issues no warning with the statement above. In Pascal
>
> d := a = b AND c
>
> means
>
> d := a = (b AND c)


gcc, like any compiler, issues whatever warnings its authors thought
worth issuing (and spent the time to implement). There's no
requirement that they be consistent, as long as the compiler issues
all diagnostics required by the standard.

If you want to discuss the choices the authors made, try gnu.gcc.bug
or gnu.gcc.help.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      11-21-2005
In article <(E-Mail Removed)> Jordan Abel <(E-Mail Removed)> writes:
> On 2005-11-20, August Karlstrom <(E-Mail Removed)> wrote:
> > Hi,
> > Can someone explain the reason for the warning from GCC below:
> >
> > $ gcc -Wall test.c
> > test.c: In function ‘main’:
> > test.c:8: warning: suggest parentheses around && within ||
> >
> > </shell-session>
> >
> > For sake of consistency, why not suggest parentheses around `y * z' as
> > well? (which would be absurd)

>
> Logical operators are easier for people to get confused about because
> they are less familiar.


The more so because in mathematics there is *no* precedence between and and
or. But there is between + and * (although many people even get that wrong.)
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      11-21-2005
Dik T. Winter wrote:
> In article <(E-Mail Removed)> Jordan Abel <(E-Mail Removed)> writes:
> > On 2005-11-20, August Karlstrom <(E-Mail Removed)> wrote:
> > > Hi,
> > > Can someone explain the reason for the warning from GCC below:
> > >
> > > $ gcc -Wall test.c
> > > test.c: In function ‘main’:
> > > test.c:8: warning: suggest parentheses around && within ||
> > >
> > > </shell-session>
> > >
> > > For sake of consistency, why not suggest parentheses around `y * z' as
> > > well? (which would be absurd)

> >
> > Logical operators are easier for people to get confused about because
> > they are less familiar.

>
> The more so because in mathematics there is *no* precedence between and and
> or. But there is between + and * (although many people even get that wrong.)


When I studied Boolean algebra, we certainly did consider /\ (and) to
have greater precedence than \/ (or).

--
Simon.
 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      11-21-2005
Simon Biber wrote:
<snip>
> When I studied Boolean algebra, we certainly did consider /\ (and) to
> have greater precedence than \/ (or).
>

This is common in books and courses oriented specifically towards
computer science, because programming languages do it (not all, but many).

In mathematics generally this is virtually unheard of, because
conjunction and disjunction are each other's duals, and assigning one
higher priority than the other seems unnatural.

S.
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      11-21-2005
Dik T. Winter wrote:
> In article <(E-Mail Removed)> Jordan Abel <(E-Mail Removed)> writes:
> > On 2005-11-20, August Karlstrom <(E-Mail Removed)> wrote:
> > > Hi,
> > > Can someone explain the reason for the warning from GCC below:
> > >
> > > $ gcc -Wall test.c
> > > test.c: In function ‘main’:
> > > test.c:8: warning: suggest parentheses around && within ||
> > >
> > > </shell-session>
> > >
> > > For sake of consistency, why not suggest parentheses around `y * z' as
> > > well? (which would be absurd)

> >
> > Logical operators are easier for people to get confused about because
> > they are less familiar.

>
> The more so because in mathematics there is *no* precedence between and and
> or. But there is between + and * (although many people even get that wrong.)


OK, that's probably a good reason for not assuming any particular
precedence. In Ada `and' and `or' have the same precedence. In all
other common imperative languages I have checked "and" has higher
precedence than "or". Oberon even uses & for "and" and OR for "or" to
make "and" stand out less than "or" and hence emphasize the difference
in precedence.


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Another logical precedence question -j b- Ruby 4 07-07-2008 04:00 PM
precedence of binary operators arnuld C++ 12 07-27-2007 02:36 PM
Precedence of operators harry C Programming 9 05-05-2007 06:43 PM
Logical operator precedence & associativity marko C Programming 9 11-13-2006 02:27 PM
Precedence of overloaded cast operators Ross C++ 3 10-31-2006 03:38 PM



Advertisments