为什么它会为代码段建议括号? [英] Why does it suggest parentheses for the code snippet?
问题描述
第2行的代码是否正确?
a.c:2:警告:建议围绕&&的括号在||
int isleap(int year){
if(year%4 == 0&& year%100!= 0 ||年%400 == 0)/ *行
2 * /
返回1;
返回0;
}
2月14日17:16,lovecreatesbea ... @ gmail.com
< lovecreatesbea ... @ gmail.comwrote:
第2行的代码是否正确?
是的。这是正确的,但它并不难看。
a.c:2:警告:建议括号围绕&&在||
您的编译器有很好的品味:-)它是哪一个?
int isleap(int year ){
if(year%4 == 0&& year%100!= 0 || year%400 == 0)/ * line
2 * /
返回1;
返回0;
}
返回(!(年份%4)&&(年%100)||!(年%
400))的奖励积分;"当然?
文章< 11 ********************** @ a34g2000cwb .googlegroups .com>,
lovecreatesbea ... @ gmail.com< lo *************** @ gmail.comwrote:
< blockquote class =post_quotes>
>第2行的代码是否正确?
> a.c:2:警告:建议围绕&&的括号在||
> int isleap(int year){
if(year%4 == 0&& year; %100!= 0 ||年%400 == 0)/ *行
2 * /
返回1;
返回0;
}
&&绑定比||更紧密,因此测试等同于
(((年%4 == 0)&&(年%100!= 0))|| (年%400 == 0))
然而,人们错误地想到
&&和||由于具有相同的优先级,因此人们从左到右阅读运算符是很常见的。在这种特殊情况下,它不会产生差异,但是如果你已经写过,那么如果(年%400 == 0 ||年),则需要
%4 == 0&& year%100!= 0)
然后人们倾向于(错误地)将其视为
if((年%400 == 0 ||年%4 == 0)&&年%100!= 0)
真的是什么时候
if(年%400 == 0 ||(年%4 == 0&年%100!= 0))
警告正在改变您对常见误读的可能性。
-
有什么可以说的,看,这是新的吗?它已经过时了,这已经是我们面前的了。 - Ecclesiastes
" lovecreatesbea ... @ gmail.com" < lo *************** @ gmail.comwrites:
第2行的代码是否正确?
ac:2:警告:建议围绕&&的括号在||
int isleap(int year){
if(year%4 == 0&& year%100!= 0 ||年%400 == 0)/ *行
2 * /
返回1;
返回0;
}
编号这是在FAQ中。
20.32:2000年是闰年吗?是(年%4 == 0)准确的测试
闰年?
答:是和否分别。目前
格里高利历的完整表达是
年%4 == 0&& (年%100!= 0 ||年%400 == 0)
有关详细信息,请参阅优秀的天文年历或其他参考资料。
(要预防一个永恒的辩论:声称存在4000年规则的参考文献是错误的。)参见问题
13.14和13.14b。
>
-
Peter Seebach在C99:
[F]或大部分功能已添加,未删除。这听起来很棒
很棒,直到你试着在一天内完成大小的标准打印输出
。
>
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;
}
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?
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
"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."
这篇关于为什么它会为代码段建议括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!