&安培;&安培;和||运营商优先执行 [英] && and || Operator precedence enforcement

查看:83
本文介绍了&安培;&安培;和||运营商优先执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计,

这个问题之前可能已经被问到并得到了解答。但是,抱歉

我无法从档案中找到一个。


我输入了这个程序并用gcc编译了3.3.2

main(){

int i = -3,j = 2,k = 0,m;


m = ++ i || ++ j&& ++ k;

printf(" \ n%d%d%d%d \ n",i,j,k,m);

}

并得到输出

-2 2 0 1


我认为答案应该是

-3 3 1 1


作为++ j和++ k首先进行评估&&运算符应用于

获得结果为TRUE(3&& 1)。因此,++我不会被评估为
作为||的一部分是真的。


ANSI C规范明确指出&&优先于||。


有人可以解释为什么这个奇怪的-2 2 0 1程序输出

获得?


干杯

Vivek

Hi Folks,
This question may well have been asked and answered before. But, sorry
that I couldn''t find one from the archives.

I typed up this program and compiled it with gcc 3.3.2
main() {
int i = -3,j= 2,k=0,m;

m = ++i || ++j && ++k;
printf("\n%d %d %d %d\n",i,j,k,m);
}
and got the output to be
-2 2 0 1

I presumed that the answer should have well been
-3 3 1 1

as ++j and ++k would first be evaluated and && operator applied to
obtain the result to be TRUE (3 && 1). Hence, ++i wouldn''t be evaluated
as one part of || goes TRUE.

The ANSI C specification clearly states that && has precedence over ||.

Could someone explain why this strange "-2 2 0 1" output for the program
is obtained?

Cheers
Vivek

推荐答案

Vivek N写道:
Vivek N wrote:

嗨伙计,
这个问题可能已经被问及以前的答案。但是,抱歉
我无法从档案中找到一个。

我输入了这个程序并用gcc编译了3.3.2
main(){
int i = -3,j = 2,k = 0,m;

m = ++ i || ++ j&& ++ k;
printf(" \ n%d%d%d%d \ n",i,j,k,m);
}
并得到输出是
-2 2 0 1

我认为答案应该是很好的,因为++ j和首先评估++ k并且&&运算符应用于
获得结果为TRUE(3&& 1)。
因此,++我不会被评估为||的一部分是真的。

ANSI C规范明确指出&&
优先于||。


(a || b&& c),与(a ||(b&& c))相同。

有人可以解释为什么这个奇怪的-2 2 0 1
节目输出?

Hi Folks,
This question may well have been asked and answered before. But, sorry
that I couldn''t find one from the archives.

I typed up this program and compiled it with gcc 3.3.2
main() {
int i = -3,j= 2,k=0,m;

m = ++i || ++j && ++k;
printf("\n%d %d %d %d\n",i,j,k,m);
}
and got the output to be
-2 2 0 1

I presumed that the answer should have well been
-3 3 1 1

as ++j and ++k would first be evaluated and && operator applied to
obtain the result to be TRUE (3 && 1).
Hence, ++i wouldn''t be evaluated
as one part of || goes TRUE.

The ANSI C specification clearly states that &&
has precedence over ||.
(a || b && c), is the same as (a || (b && c)).
Could someone explain why this strange "-2 2 0 1"
output for the program is obtained?




j和k根本没有评估。


||的左操作数在右边之前进行评估。

||的右操作数只有当左操作数等于零时才有条件地评估。

你的左操作数(++ i),不等于零。

你的右操作数(++ j&& ++ k)未被评估。


-

pete



j and k are not evaluated at all.

The left operand of || is evaluted before the right.
The right operand of || is evaluated conditionally only if
the left operand is equal to zero.
Your left operand (++i), is not equal to zero.
Your right operand (++j && ++k) is not evaluated.

--
pete


Vivek N< ke *********** @ yahoo.com>这样说:
Vivek N <ke***********@yahoo.com> spoke thus:
main(){


允许函数的返回类型默认为int是非法的

C99,否则可疑。

int i = -3,j = 2,k = 0,m;
m = ++ i || ++ j&& ++ k;
printf(" \ n%d%d%d%d \ n,i,j,k,m);


哪里是< stdio.h>?打开gcc'的警告(-Wall -pedantic -ansi)。

有人可以解释为什么这个奇怪的-2 2 0 1获得该程序的输出
main() {
Allowing the return type of a function to default to int is illegal in
C99 and dubious otherwise.
int i = -3,j= 2,k=0,m; m = ++i || ++j && ++k;
printf("\n%d %d %d %d\n",i,j,k,m);
Where is <stdio.h>? Turn up gcc''s warnings (-Wall -pedantic -ansi).
Could someone explain why this strange "-2 2 0 1" output for the program
is obtained?




由于短路评估,一点都不奇怪。

&&优先于||只表示对m的赋值是

相当于


m = ++ i || (++ j& ++ k);


++我首先计算,如果它是非零(真),那么整个
$ b无论是什么(++ j&& ++ k),$ b语句都保证为真。

在C中,这意味着(++ j&& ++ k )不会被评估。那是什么短路评估是什么?这就是为什么只有我在你发布的代码中增加了




-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



Not strange at all, thanks to short-circuit evaluation. The fact that
&& has precedence over || only means that the assignment to m is
equivalent to

m=++i || (++j && ++k);

++i is computed first, and if it is nonzero (true), then the entire
statement is guaranteed to be true regardless of what (++j && ++k) is.
In C, this means that (++j && ++k) will not be evaluated. That is
what short-circuit evaluation is, and that is why only i is
incremented in the code you posted.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


ke ****** *****@yahoo.com (Vivek N)写道:
ke***********@yahoo.com (Vivek N) wrote:
作为++ j和++ k将首先被评估和&&运算符应用于
获得结果为TRUE(3&& 1)。因此,++我不会被评估为||的一部分是真的。

ANSI C规范明确指出&&优先于||。
as ++j and ++k would first be evaluated and && operator applied to
obtain the result to be TRUE (3 && 1). Hence, ++i wouldn''t be evaluated
as one part of || goes TRUE.

The ANSI C specification clearly states that && has precedence over ||.




ISO C规范没有说明这样的事情,因为优先级是是什么?b $ b不明确。

它是什么_does_状态是这样的:

*首先,所有逻辑运算符都是从左到右计算的,用

进行短路评估。这意味着它可以这样做:


if(ptr!= NULL&& * ptr!=''A'')...


并确保在被解除引用之前测试指针。

*逻辑和(&&)运算符比逻辑或

(||)。也就是说,x || y&& z表示x || (y&& z),而不是(x || y)&& ž。然而,

评价顺序保持不变。


Richard



The ISO C specification states no such thing, since "precedence" is
ambiguous.
What it _does_ state is this:
* To begin with, all logical operators are evaluated left-to-right, with
short-circuit evaluation. This means that it''s possible to do this:

if (ptr!=NULL && *ptr!=''A'') ...

and be certain that your pointer is tested before being dereferenced.
* The logical and (&&) operator binds more closely than the logical or
(||). That is, x || y && z means x || (y && z), not (x || y) && z. The
order of evaluation remains the same, however.

Richard


这篇关于&安培;&安培;和||运营商优先执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆