473,395 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Why does it suggest parentheses for the code snippet?

Isn't code at line 2 correct?

a.c:2: warning: suggest parentheses around && within ||

int isleap(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) /*line
2*/
return 1;
return 0;
}

Feb 14 '07 #1
18 15090
On 14 Feb, 17:16, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
Isn't code at line 2 correct?
Yeah. It's correct, but it's not ugly.
a.c:2: warning: suggest parentheses around && within ||
Your compiler has good taste :-) Which one is it?
int isleap(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) /*line
2*/
return 1;
return 0;

}
Bonus points for "return(!(year % 4) && (year % 100) || !(year %
400));" surely?

Feb 14 '07 #2
In article <11**********************@a34g2000cwb.googlegroups .com>,
lovecreatesbea...@gmail.com <lo***************@gmail.comwrote:
>Isn't code at line 2 correct?
>a.c:2: warning: suggest parentheses around && within ||
>int isleap(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) /*line
2*/
return 1;
return 0;
}
&& binds more tightly than ||, so the test is equivilent to

(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))

However, it is relatively common for people to mistakenly think of
&& and || as having the same precedences, and so it is common
for people to read the operators from left to right. It doesn't
make a difference in this particular case, but if you had written,

if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)

then people would tend to (mistakenly) read this as

if ((year % 400 == 0 || year % 4 == 0) && year % 100 != 0)

when really it is

if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))

The warning is altering you to the possibility of the common misreading.

--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Feb 14 '07 #3
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
Isn't code at line 2 correct?

a.c:2: warning: suggest parentheses around && within ||

int isleap(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) /*line
2*/
return 1;
return 0;
}
No. This is in the FAQ.

20.32: Will 2000 be a leap year? Is (year % 4 == 0) an accurate test
for leap years?

A: Yes and no, respectively. The full expression for the present
Gregorian calendar is

year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)

See a good astronomical almanac or other reference for details.
(To forestall an eternal debate: references which claim the
existence of a 4000-year rule are wrong.) See also questions
13.14 and 13.14b.

--
Peter Seebach on C99:
"[F]or the most part, features were added, not removed. This sounds
great until you try to carry a full-sized printout of the standard
around for a day."
Feb 14 '07 #4
On Feb 14, 5:31 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrites:
Isn't code at line 2 correct?
a.c:2: warning: suggest parentheses around && within ||
int isleap(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) /*line
2*/
return 1;
return 0;
}

No. This is in the FAQ.

20.32: Will 2000 be a leap year? Is (year % 4 == 0) an accurate test
for leap years?

A: Yes and no, respectively. The full expression for the present
Gregorian calendar is

year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
The expressions are different, but always produce the same result.

Feb 14 '07 #5
"christian.bau" <ch***********@cbau.wanadoo.co.ukwrites:
On Feb 14, 5:31 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
>"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrites:
Isn't code at line 2 correct?
a.c:2: warning: suggest parentheses around && within ||
int isleap(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) /*line
2*/
return 1;
return 0;
}

No. This is in the FAQ.

20.32: Will 2000 be a leap year? Is (year % 4 == 0) an accurate test
for leap years?

A: Yes and no, respectively. The full expression for the present
Gregorian calendar is

year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)

The expressions are different, but always produce the same result.
For the record, that's why I canceled my article quoted above
soon after posting it.
--
Here's a tip: null pointers don't have to be *dull* pointers!
Feb 14 '07 #6
On Feb 15, 6:36 am, mark_blue...@pobox.com wrote:
>
Bonus points for "return(!(year % 4) && (year % 100) || !(year %
400));" surely?
Not really: your code would still trigger the warning in the OP,
and you have superfluous brackets around the expression being
returned.
Feb 15 '07 #7
On 2ÔÂ15ÈÕ, ÉÏÎç1ʱ31·Ö, Ben Pfaff <b...@cs.stanford.eduwrote:
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrites:
Isn't code at line 2 correct?
a.c:2: warning: suggest parentheses around && within ||
int isleap(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) /*line
2*/
return 1;
return 0;
}

No. This is in the FAQ.
year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
The code in my last post is from K&R2, isn't K&R right?

Feb 15 '07 #8
Ben Pfaff said:
"christian.bau" <ch***********@cbau.wanadoo.co.ukwrites:
<leap year expressions snipped>
>>
The expressions are different, but always produce the same result.

For the record, that's why I canceled my article quoted above
soon after posting it.
"Anyone who thinks programming dates is easy to get right the first time
probably hasn't done much of it." - Peter van der Linden

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 15 '07 #9

"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote in message
news:11**********************@a34g2000cwb.googlegr oups.com...
Isn't code at line 2 correct?
Its not obvious from a first glance if it is or is not correct. Whilst the
precedance of arithmetic operators is easily remembered, when you mix
arithmetic and comparative operators the order of evaluation is not clear.
Inserting parenthesis makes this clear.
a.c:2: warning: suggest parentheses around && within ||

int isleap(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) /*line
2*/
return 1;
return 0;
}

Feb 17 '07 #10

"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.cawrote in message
news:eq**********@canopus.cc.umanitoba.ca...
In article <11**********************@a34g2000cwb.googlegroups .com>,
lovecreatesbea...@gmail.com <lo***************@gmail.comwrote:
Isn't code at line 2 correct?
a.c:2: warning: suggest parentheses around && within ||
int isleap(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) /*line
2*/
return 1;
return 0;
}

&& binds more tightly than ||, so the test is equivilent to

(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))

However, it is relatively common for people to mistakenly think of
&& and || as having the same precedences, and so it is common
for people to read the operators from left to right. It doesn't
make a difference in this particular case, but if you had written,

if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)

then people would tend to (mistakenly) read this as

if ((year % 400 == 0 || year % 4 == 0) && year % 100 != 0)

when really it is

if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))

The warning is altering you to the possibility of the common misreading.
Along the same lines GCC suggests parenthesis around constructs of the form

if( x=mysub(a,b,c) ) {

Any thoughts on why? It seems obvious to me that this is assigning the
return from mysub to x and then testing it...
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes

Feb 17 '07 #11
"David Wade" <g8***@yahoo.comwrites:
"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.cawrote:
>The warning is altering you to the possibility of the common misreading.

Along the same lines GCC suggests parenthesis around constructs of the form

if( x=mysub(a,b,c) ) {

Any thoughts on why? It seems obvious to me that this is assigning the
return from mysub to x and then testing it...
For the same reason: while it's valid code, it differs from
if( x==mysub(a,b,c) ) {
by just one character and the two have very different behaviour. The
presence of parens around the assignment is the heuristic GCC uses to
determine whether to warn you about the possibility that your code
contained a common typo.

mlp
Feb 17 '07 #12
David Wade wrote:
>
.... snip ...
>
Along the same lines GCC suggests parenthesis around constructs
of the form

if( x=mysub(a,b,c) ) {

Any thoughts on why? It seems obvious to me that this is assigning
the return from mysub to x and then testing it...
Because the original intent might have been:

if (x == mysub(a, b, c)) {

and a typo has deleted the second '='. This is a fairly common
error. By adding the extra set of parenthesis the test is on a
single value, i.e. the parenthized expression. This sort of typo
is also the reason for prefering:

if (CONST == var)
rather than:
if (var == CONST)

because dropping one of the '=' chars will trigger an error in the
first case, but not in the second. Unlike the extra parens case,
this will catch the error on any compiler.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 18 '07 #13
CBFalconer <cb********@yahoo.comwrites:
[...]
This sort of typo
is also the reason for prefering:

if (CONST == var)
rather than:
if (var == CONST)

because dropping one of the '=' chars will trigger an error in the
first case, but not in the second. Unlike the extra parens case,
this will catch the error on any compiler.
Yes. On the other hand, some of us find the "CONST == var" form so
ugly that it's not worth the increase in safety. De gustibus yadda
yadda.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Feb 18 '07 #14

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
CBFalconer <cb********@yahoo.comwrites:
[...]
This sort of typo
is also the reason for prefering:

if (CONST == var)
rather than:
if (var == CONST)

because dropping one of the '=' chars will trigger an error in the
first case, but not in the second. Unlike the extra parens case,
this will catch the error on any compiler.

Yes. On the other hand, some of us find the "CONST == var" form so
ugly that it's not worth the increase in safety. De gustibus yadda
yadda.
I found the extra parenthisis round my expression ugly. Some one who was
reviewing the code suggested changing it to say

if (NULL != ( f=myfunc(a,b,c) ) )

which I found very ugly, and unintuitive. The suggestion was that by putting
a comparision in I was making it obviusly a conditional. My view was that I
was free to use conditionals as and when appropriate ....
--
Keith Thompson (The_Other_Keith) ks***@mib.org
<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.

Feb 18 '07 #15
"David Wade" <g8***@yahoo.comwrote:
"Keith Thompson" <ks***@mib.orgwrote in message
CBFalconer <cb********@yahoo.comwrites:
This sort of typo
is also the reason for prefering:
>
if (CONST == var)
rather than:
if (var == CONST)
>
because dropping one of the '=' chars will trigger an error in the
first case, but not in the second. Unlike the extra parens case,
this will catch the error on any compiler.
Yes. On the other hand, some of us find the "CONST == var" form so
ugly that it's not worth the increase in safety. De gustibus yadda
yadda.
Besides, like setting free()d pointers to null, it's a trap. One day you
_will_ accidentally write if (var1 = var2) (sorry, presumably you'll
write if (var2 = var1)), and you'll have dishabited yourself from
spotting such errors. You will proceed to make yourself look silly in
front of your colleagues who haven't... if you're lucky.
I found the extra parenthisis round my expression ugly. Some one who was
reviewing the code suggested changing it to say

if (NULL != ( f=myfunc(a,b,c) ) )

which I found very ugly, and unintuitive.
Urg. Why not write

if ( ( NULL != ( f=myfunc(a,b,c) ) ) == TRUE )

Richard

Feb 19 '07 #16
Richard Bos wrote:
"David Wade" <g8***@yahoo.comwrote:
>"Keith Thompson" <ks***@mib.orgwrote in message
>>CBFalconer <cb********@yahoo.comwrites:
This sort of typo
is also the reason for prefering:

if (CONST == var)
rather than:
if (var == CONST)

because dropping one of the '=' chars will trigger an error in the
first case, but not in the second. Unlike the extra parens case,
this will catch the error on any compiler.
Yes. On the other hand, some of us find the "CONST == var" form so
ugly that it's not worth the increase in safety. De gustibus yadda
yadda.

Besides, like setting free()d pointers to null, it's a trap. One day you
_will_ accidentally write if (var1 = var2) (sorry, presumably you'll
write if (var2 = var1)), and you'll have dishabited yourself from
spotting such errors. You will proceed to make yourself look silly in
front of your colleagues who haven't... if you're lucky.
>I found the extra parenthisis round my expression ugly. Some one who was
reviewing the code suggested changing it to say

if (NULL != ( f=myfunc(a,b,c) ) )

which I found very ugly, and unintuitive.

Urg. Why not write

if ( ( NULL != ( f=myfunc(a,b,c) ) ) == TRUE )
Some people don't like doing "if (ptr)", they insist that
if (ptr != NULL)
is the right way, and if you have this, you combine it with
if (0 == foo)
and there you go:
if (NULL != (f = myfunc(a,b,c)))
Crazy stuff.

Yevgen
Feb 19 '07 #17
Richard Bos wrote:
"David Wade" <g8***@yahoo.comwrote:
I found the extra parenthisis round my expression ugly. Some one who was
reviewing the code suggested changing it to say

if (NULL != ( f=myfunc(a,b,c) ) )

which I found very ugly, and unintuitive.

Urg. Why not write

if ( ( NULL != ( f=myfunc(a,b,c) ) ) == TRUE )
I know you're not serious, but because (NULL != ( f=myfunc(a,b,c) ) )
is already easily readable as a boolean expression, arguably unlike
( f=myfunc(a,b,c) ).

Feb 19 '07 #18
"=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=" <tr*****@gmail.comwrote:
Richard Bos wrote:
"David Wade" <g8***@yahoo.comwrote:
I found the extra parenthisis round my expression ugly. Some one who was
reviewing the code suggested changing it to say
>
if (NULL != ( f=myfunc(a,b,c) ) )
>
which I found very ugly, and unintuitive.
Urg. Why not write

if ( ( NULL != ( f=myfunc(a,b,c) ) ) == TRUE )

I know you're not serious, but because (NULL != ( f=myfunc(a,b,c) ) )
is already easily readable as a boolean expression, arguably unlike
( f=myfunc(a,b,c) ).
No, I fear not. You are right that I'm not serious, but that's because I
find my proposed alternative no more unreadable than the one which was
suggested to David, and rather less readable than the simple, idiomatic
if (f=myfunc(a, b, c))

Richard
Feb 22 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andy Gayton | last post by:
Hey all, It looks like dynamic changes to a html document are only redrawn when the current code snippet comes to an end. For example the following page changes the text in a span, waits for a...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
2
by: sck10 | last post by:
Hello, I went to the Tools menu and looked for the Code Snippet Manager, but it was not there. How do I go about adding it to my menu? -- Thanks in advance, sck10
0
by: Seigfried | last post by:
There seems to be at least three competing Intellisense Code Snippet Editors available from Microsoft supported site: 1 - VB Snippet Editor (SnippetEditor.exe) - downloaded from Microsoft page ...
1
by: Gary Brown | last post by:
Hi and Happy Holidays, While typing in a member function an errant finger triggered a bit of code snippet that throws a "not yet implemented" exception if the member is called. It happened...
2
by: daokfella | last post by:
Hi all, I've created a code snippet in VS 2005 and saved it in the proper snippet folder. When I right-click and select Insert Snippet, I navigation to My Code Snippets and it shows my snippet....
6
by: natkw1 | last post by:
Hi all, I'm new to C and kind of confused on this. I've had a look around this group for suggestions but still not sure why the warning is occurring. What I've done is this <code snip> void...
3
by: =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno | last post by:
Could you advise me if there could be any memory leak in this C# code snippet?: http://pastebin.com/m2d2ded2d As I understand, I have closed all risky objects: EventWaitHandle, and Timer. Do...
4
by: tshad | last post by:
I was watching a video that used a code snippet to create a property and when you type "prop" tab tab, it would create the private variable as well as the property definitions with the private...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.