Fundamental question

B

Ben Pfaff

Guillaume said:
Is it? I'm not sure you understood what I meant by that.

If && and || don't have equal priority, which of them
has higher priority then? Good luck. ;-)

You're using an undefined term here. There's no reference to
"priority" in the C standard. I think you're thinking of
"precedence", which is at least referenced in sidelong ways in
the Standard.

The && operator has higher precedence than || does in C. You can
read the grammar given in the C standard to verify this:

logical-AND-expression:
inclusive-OR-expression
logical-AND-expression && inclusive-OR-expression

logical-OR-expression:
logical-AND-expression
logical-OR-expression || logical-AND-expression
Both logical operators guarantee left-to-right evaluation,
and that is it. This does not add up to different priority
in the algebraic sense, as in: 'A + B * C'. This is what
I meant. '*' has higher priority than '+'.

I don't think mathematicians call this priority either. It is
the "order of operations" or "precedence" of operators. Again,
&& has higher precedence than ||.
Is what I said clearer now?

No. You're not using the right terms.
 
J

J. J. Farrell

Keith Thompson said:
Guillaume said:
Binary logic operators have the same priority, as far as I know.
[...]

You're mistaken. Please consult a book or other reference before
posting misinformation.

Or, even better, skip the posting of misinformation altogether.

Mind you, since C doesn't have a concept of "priority", how do
we know he's mistaken? It depends on just what he means by it.
 
M

Mabden

Keith Thompson said:
The "&&" and "||" operators are evaluated left-to-right, with
short-circuit semantics, but that's a separate issue. For "+" and
"*", precedence is a question of which operators apply to which
operands; it's more syntactic than semantic. The precedence of the
"&&" and "||" operators is determined in exactly the same way.

K&R page #41 has an if that goes like this:

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
printf ("%d is a leap year\n", year);

So I guess the parenthesis in the && clause are unnecessary?!
It's not like K&R to be so verbose! ;-)
 
K

Keith Thompson

Mabden said:
K&R page #41 has an if that goes like this:

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
printf ("%d is a leap year\n", year);

So I guess the parenthesis in the && clause are unnecessary?!

The parentheses aren't strictly necessary, in the sense that the
expression would have the same semantics without them. But it's
perfectly sensible to add them when they make the code easier to read
-- especially in code intended to be read by beginners.
 
C

Chris Dollin

Guillaume said:
Is it? I'm not sure you understood what I meant by that.

I'm pretty sure I did.
If && and || don't have equal priority, which of them
has higher priority then? Good luck. ;-)

&&. What, you thought I didn't *know*?
Both logical operators guarantee left-to-right evaluation,
and that is it. This does not add up to different priority
in the algebraic sense, as in: 'A + B * C'.

It's true that order of evaluation doesn't "add up to different
priority in the algebraic sense". That doesn't mean that && and
|| don't have different priorities.
This is what I meant. '*' has higher priority than '+'.

Just as && has a higher priority (is more binding than, has shorter
scope than) ||.
Is what I said clearer now?

No, it's just as clear, and still wrong.

If && and || had the same priority, then

a || b && c

would mean [assuming the usual left association]

(a || b) && c

but - as I pointed out last time - it doesn't; the two expressions
have different values when a=true and c=false. You failed to address
this point. Just for kicks:

#include <stdio.h>

int main(void)
{
int a, b, c;
for (a = 0; a < 2; a += 1)
for (b = 0; b < 2; b += 1)
for (c = 0; c < 2; c += 1)
if ((a || b && c) != ((a || b) && c))
printf( ">> differ when a=%d, b=%d, c=%d\n", a, b, c );
return 0;
}
 
D

Dan Pop

In said:
Is it? I'm not sure you understood what I meant by that.

If && and || don't have equal priority, which of them
has higher priority then? Good luck. ;-)

Both logical operators guarantee left-to-right evaluation,
and that is it. This does not add up to different priority
in the algebraic sense, as in: 'A + B * C'. This is what
I meant. '*' has higher priority than '+'.

Is what I said clearer now?

Yup, it's clear that you're dead wrong. && has higher precedence than ||
in exactly the same way '*' has higher precedence than '+'. In other
words, the expression A || B && C is evaluated as A || (B && C) and not
as (A || B) && C. The shortcircuiting feature of the && and || operators
will suppress the evaluation of parts of the expression, if they are
irrelevant to the final result, but this doesn't affect the relative
precedence of the two operators. The order of evaluation of the operands
of A || B && C is strictly defined, while the order of evaluation of the
operands of A + B * C is not, but this has nothing to do with operator
precedence.

Dan
 
D

Dale Henderson

Mabden> K&R page #41 has an if that goes like this:

Mabden> if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
Mabden> printf ("%d is a leap year\n", year);

Mabden> So I guess the parenthesis in the && clause are
Mabden> unnecessary?! It's not like K&R to be so verbose! ;-)

Extraordinarily verbose considering my code for the same thing
often looks like:

if( !(year%4)&&year%100||!(year%400))
printf ("%d is a leap year\n",year);

If && and || had the same precedence the parenthesis would still
be unnecessary.

K&R2 page #53 has a precedence table which clearly shows && to
have the higher priority.

(Before reading this thread, I thought && and || had the same
precedence. I'm better now.)
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top