为什么没有遵循优先权? [英] Why Precedence is not followed?

查看:83
本文介绍了为什么没有遵循优先权?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

见代码....

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

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

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


我在gcc上执行了这段代码。我得到的o / p是: -

-2 3 0 1


现在我的问题是为什么k没有增加?按照优先顺序

of C,首先,所有的单向增量都应该生效。但是

在上面,第一个逻辑运算符被评估并且作为左边的

逻辑OR是1,k不递增?那么C'的优先级是不是

跟着这里? Plz帮助????????

Hello all,
See the code ....
int i=-3,j=2,k=0,m;
m=++i;&&++j||++k;
printf ("%d %d %d %d",i,j,k,m);

I executed this code on gcc. the o/p i had got is:-
-2 3 0 1

Now my question is why k is not getting incremented? As per precedence
of C, first of all, All the unirary increments should take effect. but
in above, first logical operator is evaluated and as left hand side of
logical OR is 1, k do not increment? Then is C''s precedence is not
followed here? Plz help????????

推荐答案

Rupesh写道:
大家好,
看代码....

int i = -3,j = 2,k = 0,m;
m = ++ i;&& ++ Ĵ|| ++ K表;
^ ?? printf("%d%d%d%d",i,j,k,m);

我在gcc上执行了这段代码。我得到的o / p是: -
-2 3 0 1

现在我的问题是为什么k没有增加?按照C的优先顺序,首先,所有的单一增量都应该生效。但是在上面,第一个逻辑运算符被评估,并且逻辑OR的左侧是1,k不递增?那么C'的优先级是不是在这里跟着? Plz帮助????????


参见: http://www.eskimo.com/~scs/C-faq/q3.5.html


另外(来自K& R,A7。 14 / A7.15):

对于&& ;:评估第一个操作数,包括所有副作用;如果

等于0,则表达式的值为0.否则,将评估正确的
操作数,如果它等于0,则表达式为'的价值是

0,否则为1.

对于||:评估第一个操作数,包括所有副作用;如果
它不等于0,则表达式的值为1.否则,将评估

右操作数,如果它不等于0,则表达式为' 's
值是1,否则为0.


所以行为完全正确,表达式k ++没有被评估。

亲切的问候,

Johan


-

ooooooo。 。 。 _____J_o_h_a_n___B_o_r_k_h_u_i_s___

o _____ || http://www.borkhuis.com |

。] [__n_n_ | DD [==== _____ | jo***@borkhuis.com |(________ | __ | _ [_________] _ | ________________________________ |
Hello all,
See the code ....
int i=-3,j=2,k=0,m;
m=++i;&&++j||++k; ^?? printf ("%d %d %d %d",i,j,k,m);

I executed this code on gcc. the o/p i had got is:-
-2 3 0 1

Now my question is why k is not getting incremented? As per precedence
of C, first of all, All the unirary increments should take effect. but
in above, first logical operator is evaluated and as left hand side of
logical OR is 1, k do not increment? Then is C''s precedence is not
followed here? Plz help????????
See: http://www.eskimo.com/~scs/C-faq/q3.5.html

Also (from K&R, A7.14/A7.15):
For &&: the first operand is evaluated, including all side effects; if
it is equal to 0, the value of the expression is 0. Otherwise, the right
operand is evaluated, and if it is equal to 0, the expression''s value is
0, otherwise 1.
For ||: the first operand is evaluated, including all side effects; if
it is unequal to 0, the value of the expression is 1. Otherwise, the
right operand is evaluated, and if it is unequal to 0, the expression''s
value is 1, otherwise 0.

So the behaviour is completely correct, the expression k++ is not evaluated.

Kind regards,
Johan

--
o o o o o o o . . . _____J_o_h_a_n___B_o_r_k_h_u_i_s___
o _____ || http://www.borkhuis.com |
.][__n_n_|DD[ ====_____ | jo***@borkhuis.com |(________|__|_[_________]_|________________________________|


_ / oo OOOOO oo` ooo ooo''o!o!oo!o!o`

== VxWorks FAQ: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ==


_/oo OOOOO oo` ooo ooo ''o!o!o o!o!o`
== VxWorks FAQ: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ==


" Rupesh"< ru *********** @ yahoo.com>写道:
"Rupesh" <ru***********@yahoo.com> writes:
大家好,
参见代码....

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

我在gcc上执行了这段代码。 / pi得到的是: -
-2 3 0 1

现在我的问题是为什么k没有增加?按照C的优先级,首先,全部单向增量应该生效。但是在上面,第一个逻辑运算符被评估并作为
逻辑OR是1,k不递增?那么C'的优先级是不是在这里跟着? Plz帮助????????
Hello all,
See the code ....
int i=-3,j=2,k=0,m;
m=++i;&&++j||++k;
printf ("%d %d %d %d",i,j,k,m);

I executed this code on gcc. the o/p i had got is:-
-2 3 0 1

Now my question is why k is not getting incremented? As per precedence
of C, first of all, All the unirary increments should take effect. but
in above, first logical operator is evaluated and as left hand side of
logical OR is 1, k do not increment? Then is C''s precedence is not
followed here? Plz help????????




您发布的代码包含语法错误,这使得思考

it''不是您编译的实际代码。


请发布您编译的* exact *代码。不要重新打字;

剪切并粘贴它。并发布一个完整的(短)程序,而不是代码

片段。


要求我们猜测你的错误是没有意义的实际的

代码以及你在重新输入时引入的代码。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



The code you posted contained a syntax error, which makes the think
it''s not the actual code you compiled.

Please post the *exact* code you compiled. Don''t re-type it;
cut-and-paste it. And post a complete (short) program, not a code
fragment.

There''s no point in asking us to guess which errors are in your actual
code and which you introduced when you re-typed it.

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




Rupesh写道:

Rupesh wrote:
大家好,
看到代码....

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

我在gcc上执行了这段代码。我得到的o / p是: -
-2 3 0 1

现在我的问题是为什么k没有增加?按照C的优先顺序,首先,所有的单一增量都应该生效。但是在上面,第一个逻辑运算符被评估,并且逻辑OR的左侧是1,k不递增?那么C'的优先级是不是在这里跟着? Plz帮助????????
Hello all,
See the code ....
int i=-3,j=2,k=0,m;
m=++i;&&++j||++k;
printf ("%d %d %d %d",i,j,k,m);

I executed this code on gcc. the o/p i had got is:-
-2 3 0 1

Now my question is why k is not getting incremented? As per precedence
of C, first of all, All the unirary increments should take effect. but
in above, first logical operator is evaluated and as left hand side of
logical OR is 1, k do not increment? Then is C''s precedence is not
followed here? Plz help????????




阅读Logical OR和逻辑和运营商从一些

好​​书中读取C.

声明,

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

甚至不编译。也许你的意思是

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


为什么k不增加,是

如果Logical OR运算符的第一个操作数与0不等,

则不计算第二个操作数。


如果第一个Logical AND运算符的操作数等于0,

不评估第二个操作数。



Read about the "Logical OR" and "Logical AND" operators from some
good book of C.
The statement,
m=++i;&&++j||++k;
doesn''t even compile. Perhaps you meant
m=++i&&++j||++k;

Reason why k in not incremented, is that
If the first operand of the Logical OR operator compares unequal to 0,
the second operand is not evaluated.

If the first operand of Logical AND operator compares equal to 0,
second operand is not evaluated.


这篇关于为什么没有遵循优先权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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