Fundamental question

M

myName

a && b || c

is evaluated as

(a && b) || c

or it is evaluated as

a && (b || c)

The theory and logic says it's (a && b) || c
Does it depends on compilar also ?
I don't think so.
In my view the 1st is the ultimate answer.
What you people say ?

Ajay
 
P

Pieter Droogendijk

a && b || c

is evaluated as

(a && b) || c

or it is evaluated as

a && (b || c)

The && operator has a higher precedence than ||. Therefore, a && b is
evaluated first. That makes that
a && b || c
is evaluated as
(a && b) || c
Does it depends on compilar also ?

No. It depends on the standard. And the standard says it's (a && b) || c.
I don't think so.

You thought right.
In my view the 1st is the ultimate answer.
What you people say ?

I say the first is the only answer.
 
J

Jarno A Wuolijoki

a && b || c
is evaluated as
(a && b) || c
or it is evaluated as
a && (b || c)

The theory and logic says it's (a && b) || c

This is correct on every conforming compiler.

I guess the logic is that with values 0 and 1, && is similar to
multiplication and || similar to (saturated) addition and this is
reflected in their relative precedence.

(possibly another useful analogy would be min/max but C doesn't
have operators for those..)
 
R

Richard Bos

Pieter Droogendijk said:
The && operator has a higher precedence than ||. Therefore, a && b is
evaluated first. That makes that
a && b || c
is evaluated as
(a && b) || c

And note: unlike what is true for most other operators, these operands
are evaluated in order, left to right, and when the outcome can be
determined from the operands that have already been evaluated, the
others won't be. For example, in

a*b - c

the order of evaluation could be: first c, then b, then a, then the
multiplication, then the subtraction. Another could be: first b, then a,
then the multiplication, then c, then the subtraction. The only things
you know about the order is that, obviously, the operands must be
evaluated before their operations are performed; and the multiplication
will happen before the subtraction. However, in

a && b || c

the order _must_ be:

evaluate a
if a, then
evaluate b
do logical and
else
result of logical and is guaranteed to be 0
b is not evaluated
if result of and is 0
evaluate c
do logical or
else
logical or is guaranteed to result in 1
c is not evaluated

It is also the case that, while a || b && c has the _value_ of a || (b
&& c), the operands are still evaluated in left-to-right order; that is,
if a is non-zero, the whole of the expression is true, and neither b nor
c need or will be evaluated.

This is a useful feature, because you can do things like

if (ptr!=NULL && *ptr<MAX_VALUE)

without worrying that *ptr might be evaluated before you've checked that
ptr is not null.

Richard
 
P

Pieter Droogendijk

And note: unlike what is true for most other operators, these operands
are evaluated in order, left to right, and when the outcome can be
determined from the operands that have already been evaluated, the
others won't be.

....this because there is a sequence point after the first operand to &&
and ||.
 
D

Dan Pop

In said:
a && b || c

is evaluated as

(a && b) || c

or it is evaluated as

a && (b || c)

The theory and logic says it's (a && b) || c
Does it depends on compilar also ?
I don't think so.
In my view the 1st is the ultimate answer.
What you people say ?

What does your C book say?

Dan
 
O

osmium

Dan said:
What does your C book say?

If you looked at 100 "C books" at random, how many times would you expect
his question to be answered? Either rightly or wrongly. If you think some
things are so basic as to be unworthy of discussion why not just simply say
so? Or even, God forbid, ignore the question.
 
D

Dan Pop

In said:
If you looked at 100 "C books" at random, how many times would you expect
his question to be answered? Either rightly or wrongly.

100. I don't expect any tutorial C book not to cover the operator
precedence or to get it so blatantly wrong.
If you think some
things are so basic as to be unworthy of discussion why not just simply say
so?

Because my actual answer also conveys the message, to the OP and to other
people reading this newsgroup, that such issues should be first solved
with a C book. In the unlikely case that the C book doesn't provide the
answer, the question should be posted here, by explicitly mentioning that
book X doesn't provide the answer.
Or even, God forbid, ignore the question.

Off topic questions should not be ignored!

There is a huge difference between looking up the answer in the book and
getting the wrong answer and posting here without looking it up in the
book. By your idiotic logic, we should encourage people to post their
most basic questions here, *instead* of looking up the answers in their
book first, because their books *may* provide the wrong answers or no
answer at all.

After reading this newsgroup for over 10 years, I am not aware of any
complaints about books getting the relative precedence of these two
operators wrong...

Dan
 
G

Guillaume

Binary logic operators have the same priority, as far as I know.
So, before considering what the compiler will do, what do YOU
understand when reading this logic statement?

As for me, it confuses the hell out of me.
Just use parentheses and get over the "fundamental" question.
 
K

Keith Thompson

Pieter Droogendijk said:
...this because there is a sequence point after the first operand to &&
and ||.

Well, not quite. It's true that there's a sequence point after the
first operand, but that alone doesn't imply that the second operand
won't be evaluated (short-circuit behavior). Conversely, though, the
existence of the sequence point is probably necessary to allow the
determination of whether the right operand needs to be evaluated.

The && and || operators are evaluated with short-circuit behavior
because the standard says so. It could as easily have stated that the
operands are evaluated in order, that there's a sequence point after
the evaluation of the left operand, and that both operands are always
evaluated (though of course that would have broken tons of existing
code).
 
K

Keith Thompson

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.
As for me, it confuses the hell out of me.
Just use parentheses and get over the "fundamental" question.

That's good advice if you're writing code, but less so if you're
reading code whose author assumed that you know the relative
precedence of the && and || operators.
 
M

Mark McIntyre

If you looked at 100 "C books" at random, how many times would you expect
his question to be answered?

about 100, if they were sensible book choices for beginners who didn't know
something like this.
Either rightly or wrongly. If you think some
things are so basic as to be unworthy of discussion why not just simply say
so?

In this case, I do think Dan was right to suggest that a book might be more
useful than usenet.
 
G

Guillaume

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. Thus, this doesn't qualify as classic
priority, and technically, yes, we can absolutely say that && and
|| have the same priority, 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.
Hence, the logic operators are not commutative in C, and cannot
be factored in an obvious way in the general case, unless no part of
the boolean expression has side-effects.

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

John Smith

Dan said:
100. I don't expect any tutorial C book not to cover the operator
precedence or to get it so blatantly wrong.




Because my actual answer also conveys the message, to the OP and to other
people reading this newsgroup, that such issues should be first solved
with a C book. In the unlikely case that the C book doesn't provide the
answer, the question should be posted here, by explicitly mentioning that
book X doesn't provide the answer.




Off topic questions should not be ignored!

There is a huge difference between looking up the answer in the book and
getting the wrong answer and posting here without looking it up in the
book. By your idiotic logic, we should encourage people to post their
most basic questions here, *instead* of looking up the answers in their
book first, because their books *may* provide the wrong answers or no
answer at all.

After reading this newsgroup for over 10 years, I am not aware of any
complaints about books getting the relative precedence of these two
operators wrong...

Dan

Dan, how many times do you kick your dog when you get home from
work at night?
 
B

Barry Schwarz

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. Thus, this doesn't qualify as classic
priority, and technically, yes, we can absolutely say that && and
|| have the same priority, albeit with a particular behavior

Any claim that && and || have the same precedence is just wrong.
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.
Hence, the logic operators are not commutative in C, and cannot
be factored in an obvious way in the general case, unless no part of
the boolean expression has side-effects.

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



<<Remove the del for email>>
 
K

Keith Thompson

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

Thanks. I don't need to, though. I've been using C for more than 10
years in professional settings. ;-)

Then why do you still seem to believe that "inary logic operators
have the same priority"? You can quibble about the distinction
between "priority" and "precedence" (though if you meant to make such
a distinction you should have made it clear in the first place).

The || and && operators have different precedence in C. Any statement
to the contrary is simply incorrect. Any C programmer needs to know
this, or at least needs to be recognize the need to look it up when
necessary.

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

That was one of the points you made, and I (mostly) agree with it.
 
C

Chris Dollin

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

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".
 
D

Dan Pop

In said:
Dan, how many times do you kick your dog when you get home from
work at night?

10 times for each question whose answer can be trivially found in K&R2
or in the FAQ. However, she seems to be an expert in these documents,
as she never asked me any such question...

Dan ;-)
 
G

Guillaume

Thus your claim of "equal priority" is refuted.

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

Keith Thompson

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. ;-)

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?

No, it isn't.

I thought you were making some distinction between priority and
precedence, but I don't think you are.

A + B * C is equivalent to A + (B * C), so "*" has higher precedence
than "+".

A || B && C is equivalent to A || (B && C), so "&&" has higher
precedence than "||".

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.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top