Re: AND and OR and parentheses
- From: Eric Sosman <eric.sosman@xxxxxxx>
- Date: Mon, 15 Aug 2005 16:17:19 -0400
rick wrote:
> 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 operator has higher precedence than OR, would the
> correct grouping be this:
> leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
> ?
Yes; your parentheses don't change the meaning of the
original. The compiler (presumably) issues the warning
because the people who wrote it felt that this is something
programmers often get wrong.
Oddly enough, in this particular case you *can't* get
it wrong! Consider the other possibility:
leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
This expression gives the same value to `leap' as the first
(equivalent) pair, even though it arrives at the value by a
different route.
--
Eric.Sosman@xxxxxxx
.
- Follow-Ups:
- Re: AND and OR and parentheses
- From: CBFalconer
- Re: AND and OR and parentheses
- References:
- AND and OR and parentheses
- From: rick
- AND and OR and parentheses
- Prev by Date: Re: AND and OR and parentheses
- Next by Date: Re: Safe version of gets
- Previous by thread: Re: AND and OR and parentheses
- Next by thread: Re: AND and OR and parentheses
- Index(es):
Relevant Pages
|
Loading