Re: Fundamental question

From: Chris Dollin (kers_at_hpl.hp.com)
Date: 06/07/04


Date: Mon, 07 Jun 2004 13:51:11 +0100

Guillaume wrote:

>>>Binary logic operators have the same priority, as far as I know.
>>
>> [...]
>>
>> You're mistaken. Please consult a book or other reference before
>
> Thanks. I don't need to, though. I've been using C for more than 10
> years in professional settings. ;-)
>
> There is a precedence scheme when using && and || logic operators
> in C, but it's not equivalent to algebraic priority, because
> it not only depends on the operators, but on the values of the
> boolean arguments as well.

Wrong. The precedence is syntactic, and [hence] independant of the
values of the operands.

> Thus, this doesn't qualify as classic
> priority, and technically, yes, we can absolutely say that && and
> || have the same priority,

No. See below.

> albeit with a particular behavior
> that, of course, must be known when using C.
>
> In particular, a && b is not functionally equivalent to b && a
> if a or b have side-effects.

Yes. This, however, to do with order-of-evaluation, *not* precedence.
Precedence [and associativity] allows us to determine that

    a || b && c means a || (b && c)
                   not (a || b) && c

which differ when (a=true and c=false), with not a side-effect
in sight, nor any dependance on short-circuit evaluation.

[Clearly if these operators right-associated, I could produce
another counter-example by switching || and &&].

Thus your claim of "equal priority" is refuted.

> The aforementioned expression was deterministic, but highly
> unreadable. This is the point I was making.

"Highly unreadable" is a rather intense way to say "potentially
confusing".

-- 
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html


Relevant Pages

  • Re: Fundamental question
    ... between "priority" and "precedence" (though if you meant to make such ... a distinction you should have made it clear in the first place). ... Any C programmer needs to know ...
    (comp.lang.c)
  • issues with SO_PRIORITY and IP_TOS
    ... tos field to generate the packet priority value. ... backwards compatible with the old precedence field. ... for vlan priority tagging there are only 3 bits available. ... The second call sets the proper socket priority so ...
    (Linux-Kernel)
  • Re: Fundamental question
    ... You're using an undefined term here. ... There's no reference to ... The && operator has higher precedence than || does in C. ... This does not add up to different priority ...
    (comp.lang.c)
  • Re: Get indexed on Google ------- Google Sitemaps (BETA)
    ... >Will it make any difference by adding the priority to the pages in the ... >google site map feed. ... It depends what timeframe you use as reference. ...
    (alt.internet.search-engines)
  • Re: Fundamental question
    ... >>Binary logic operators have the same priority, ... the boolean expression has side-effects. ...
    (comp.lang.c)