的和放precedence;&安培;在|| [英] Precedence of && over ||

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

问题描述

据我所知逻辑运算符&放大器;&安培; 具有较高的precedence比 || 。在运行code:

 的#include<&stdio.h中GT;诠释的main()
{
    INT I = 1,J = 1,K = 1;    的printf(%d个\\ n,++我|| ++ J&安培;&安培; ++ K);
    的printf(%D%D,I,J,K);    返回0;
}

是给输出:

  1
2 1 1

这是可能的,只有当 ++我|| ++ J&安培;&安培; ++氏/ code>评估是这样的:

 (++ I)|| (++ J&安培;&安培; ++ K)

但是,根据操作者precedence规则应当评估为:

 (++我|| ++ j)条和放大器;&安培; (++ K)

,因此输出应该是:

  1
2 1 2

这有什么问题呢?

注意:按照我的理解,我认为评估为更高的precedence的操作如下(如果是左关联的):结果
 1.评估其左恩pression结果
 2.然后评估其右侧前pression(如果需要)
结果
难道我错了吗?


解决方案

您说的:


  

这是可能的,只有当 ++我|| ++ J&安培;&安培; ++氏/ code>评估是这样的:

 (++ I)|| (++ J&安培;&安培; ++ K)


  
  

但是,根据操作者precedence规则应当评估为:

 (++我|| ++ j)条和放大器;&安培; (++ K)


第一组是正确的,因为的precedence &放大器;&安培; 的|| <的precedence更高/ code>。然后前pression作为一个整体评估的LHS的 || ,以递增的副作用 I ,其计算结果为真。这意味着,在 || (即&放大器;&安培; 前pression)的RHS未评估因为在所有不需要它来确定整体前pression的真理。

所以,编译器是正确的;你误会了precedence以某种方式。



  

为什么是第一组是否正确?根据第一组<​​code> || 具有较高的precedence比&放大器;&安培; 。什么是我的问题呢?


您不了解precedence,看来,还是你不理解与评价规程precedence的相互作用。第一组提供更高的precedence到&放大器;&安培;

如果您有 A + B * C ,其中 * 具有更高的precedence比 + ,那么它被评价为 A +(b * C),是不是?更改 + || * &放大器;&安培; 和前pressions同构和间pretation类似于

算术前pression和逻辑前pression之间最大的区别是,逻辑前pression的操作数都进行评估左到右,但算术前的操作数pression没有;编译器可以评估 B * C 评估 A (但必须评估 B * C之前做加法之前)。相比之下,在逻辑前pression( A || B和;和C ),编译器必须评估 A 评估之前 b和;&安培; ç,并在 A 被证明是真实的,它必须的的评价为 B C ,更不用说 b和;&安培; ç

As I know logical operator && has higher precedence than ||. On running the code:

#include <stdio.h>

int main()
{
    int i = 1, j =1, k = 1;

    printf("%d\n",++i || ++j && ++k);
    printf("%d %d %d",i,j,k);

    return 0;
}

is giving the output:

1
2 1 1

which is possible only when ++i || ++j && ++k is evaluated like this:

(++i) || (++j && ++k)

But, according to operator precedence rule it should be evaluated as:

(++i || ++j) && (++k)

and hence output should be:

1
2 1 2

What is going wrong with this?

NOTE: As per my understanding I think an operator of higher precedence evaluated as follows(if it is left associative):
1. Evaluate its left expression
2. Then evaluate its right expression(if needed)

Am I wrong?

解决方案

You say:

which is possible only when ++i || ++j && ++k is evaluated like this:

(++i) || (++j && ++k)

But, according to operator precedence rule it should be evaluated as:

(++i || ++j) && (++k)

The first grouping is correct because the precedence of && is higher than the precedence of ||. Then the expression as a whole evaluates the LHS of the ||, with the side-effect of incrementing i, which evaluates to true. That means that the RHS of the || (the && expression) is not evaluated at all because it is not needed to determine the truth of the overall expression.

So, the compiler is correct; you misunderstood precedence in some way.


Why is the first grouping correct? According to first grouping || has higher precedence than &&. What is going wrong with me?

You don't understand precedence, it seems, or you don't understand the interaction of precedence with order of evaluation. The first grouping gives higher precedence to &&.

If you have a + b * c, where * has a higher precedence than +, then it is evaluated as a + (b * c), is it not? Change + to || and * to && and the expressions are isomorphic and the interpretation is similar.

The big difference between the arithmetic expression and the logical expression is that the operands of the logical expression have to be evaluated left-to-right but the operands of the arithmetic expression do not; the compiler could evaluate b * c before evaluating a (but must evaluate b * c before doing the addition). By contrast, in the logical expression (a || b && c), the compiler must evaluate a before evaluating b && c, and when a turns out to be true, it must not evaluate either b or c, let alone b && c.

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

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