快捷方式要记住评价在C秩序和precedence运营商 [英] Shortcut to remember the Order of Evaluation and Precedence of Operators in C

查看:143
本文介绍了快捷方式要记住评价在C秩序和precedence运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么快捷方式或百威记得在C precedence和评估秩序东阳它起着主要的作用和我(或我们大多数人),通常忘记和混乱中结束。请帮我....

我想举一个例子这个......说..

 无效的主要()
{
    INT A = 1;
    A = A + + + + A;
    的printf(%d个,一);
} //输出5;无效的主要()
{
    INT A = 1;
    INT X;
    X = A + + + + A;
    的printf(%D,X);
} //输出4;

也是前pression结果
X = + A + A ++; 结果
给出了不同的结果。

我希望,如果后增量高优先级,那么 A ++ 应先进行评估,请如果IAM错误清除我,请给我解释一下它是如何工作的。


解决方案

首先,形式的前pressions A + + + + A + A + A ++ 等,导致的未定义行为;的任何的结果是可能的。从语言标准( n1256 ):


6.5防爆pressions 的结果
...结果
2 previous和下一个序列点的对象应具有其存储的值之间
由前pression的评价最多一次修改。 72)而且,前值
应仅被理解为确定的值被存储。 73)结果
...结果
72)浮点状态标志不是一个对象,并且可以多次设置一个前pression内。搜索结果
73)本款呈现不确定的语句前pressions如


        I = + I + 1;
        A [++] =我;


同时允许


        I = I + 1;
        一个[我] =我;


所以,千万不要那样做。

一个定义良好的前pression像 X = A + + + + b 会被解析为 X =((A ++)+ (++ b));在 ++ 运营商的两种形式有更高的precedence比另外,加入具有较高的precedence比分配。在结果的前任pression将是一样的 X = A +(B + 1)

其次,记得 ++ - 运营商拥有的结果的和一个副作用的,如下所示:


防爆pression结果副作用
---------- ------ -----------
       我++ I I = I + 1
       ++ I I + 1 I = I + 1
       我 - 我I = I - 1
       --i我 - 1 I = - 1

重要提示要记住:副作用不具备前pression进行评估后,立即应用;它只有下一个序列点之前被应用。这是可能的 X = A + + + + b 来进行评价如下:

  T1 =一个;
T2 = B + 1;
X = T1 + T2;
B = B + 1;
A = A + 1;

在这种情况下,更新 A B 被推迟到加法赋值为<$之后C $ C> X 。

至于precedence而言,这里是从高分到低分顺序一般:


  1. Postfix的运营商(都具有相同的precedence,因此运营商的序列进行评估左到右)

    • 数组下标运算符 []
    • 函数调用操作()
    • 组件选择运营商 - >
    • 后缀 ++ -

  2. 一元运算符(都具有相同的precedence,因此运营商的序列进行评估左到右)

    • preFIX ++ -
    • 的sizeof
    • 按位求反运算符
    • 逻辑否定运算符
    • 单目标志运营商 - +
    • 地址运算符
    • 引用操作 *

  3. 铸造前pressions 类型名称
  4. 乘法运算符 * /
  5. 添加剂经营者 + -
  6. 移位运算&LT;&LT; &GT;&GT;
  7. 关系运算符&LT; &GT; &LT; = &GT; =
  8. 相等运算符 == !=
  9. 位与
  10. 位异或 ^
  11. 位或 |
  12. 逻辑与 &&
  13. 逻辑或 ||
  14. 条件运算符
  15. 赋值运算符 = + = - = * = / = (%)= &LT;&LT; = &GT;&GT; = &= ^ = | =
  16. 顺序(逗号)运算符

所以,前pressions像 * X ++ 被解析为 *(X ++),因为后缀 ++ 具有较高的precedence比一元 * 。同样, sizeof的X + 1 被解析为(sizeof的X)+ 1 ,因为的sizeof 具有较高的precedence比加法。一名前pression像点++ - &GT; X 被解析为(P ++) - GT; X ;这两个后缀 ++ - &GT; 运营商具有相同的precedence,所以他们从左边解析以对。

这是关于短快捷键搞定;有疑问时,请使用括号。

is there any shortcut or bestway to remember the precedence and order of evaluation in C beacause it plays the main role and i or (most of us) usually forgets and end up with the mess. Please Help me....

i want to give an example for this... say..

void main()  
{  
    int a=1;  
    a = a++ + ++a;  
    printf("%d",a);  
}//prints 5; 

void main()  
{  
    int a=1;  
    int x;  
    x = a++ + ++a;  
    printf("%d",x);  
}//prints 4;  

also the expression
x = ++a + a++;
gives a different result

i hope that if post increment have high priority then a++ should be evaluated first please clear me if iam wrong and please explain me how it works..

解决方案

First of all, expressions of the form a++ + ++a, ++a + a++, etc., result in undefined behavior; any result is possible. From the language standard (n1256):

6.5 Expressions
...
2 Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.72) Furthermore, the prior value shall be read only to determine the value to be stored.73)
...
72) A floating-point status flag is not an object and can be set more than once within an expression.

73) This paragraph renders undefined statement expressions such as

        i = ++i + 1;
        a[i++] = i;

while allowing

        i = i + 1;
        a[i] = i;

So, don't do that.

A well-defined expression like x = a++ + ++b will be parsed as x = ((a++) + (++b)); both forms of the ++ operator have higher precedence than addition, and addition has higher precedence than assignment. The result of the expression will be the same as x = a + (b + 1).

Secondly, remember that the ++ and -- operators have a result and a side effect, as shown below:

Expression          Result         Side effect
----------          ------         -----------
       i++               i            i = i + 1
       ++i           i + 1            i = i + 1
       i--               i            i = i - 1
       --i           i - 1            i = i - 1

Important note to remember: the side effect doesn't have to be applied immediately after the expression is evaluated; it only has to be applied before the next sequence point. It's possible for x = a++ + ++b to be evaluated as follows:

t1 = a;
t2 = b + 1;
x = t1 + t2;
b = b + 1;
a = a + 1;  

In this case, the updates to a and b are deferred until after the addition and assignment to x.

As far as precedence is concerned, here is the general order from high to low:

  1. Postfix operators (all have the same precedence, so sequences of operators will be evaluated left-to-right)
    • array subscript operator []
    • function call operator ()
    • component selection operators . and ->
    • postfix ++ and --
  2. Unary operators (all have the same precedence, so sequences of operators will be evaluated left-to-right)
    • prefix ++ and --
    • sizeof
    • bitwise negation operator ~
    • logical negation operator !
    • unary sign operators - and +
    • address-of operator &
    • dereference operator *
  3. Cast expressions ( type name )
  4. Multiplicative operators *, /, %
  5. Additive operators + and -
  6. Shift operators << and >>
  7. Relational operators <, >, <=, >=
  8. Equality operators == and !=
  9. Bitwise AND &
  10. Bitwise XOR ^
  11. Bitwise OR |
  12. Logical AND &&
  13. Logical OR ||
  14. Conditional operator ?:
  15. Assignment operators =, +=. -=, *=, /=, %=, <<=, >>=, &=, ^=, |=
  16. Sequential (comma) operator ,

So, expressions like *x++ are parsed as *(x++), since the postfix ++ has higher precedence than the unary *. Similarly, sizeof x + 1 is parsed as (sizeof x) + 1, since sizeof has higher precedence than addition. An expression like p++->x is parsed as (p++)->x; both postfix ++ and -> operators have the same precedence, so they're parsed from left to right.

This is about as short as shortcuts get; when in doubt, use parentheses.

这篇关于快捷方式要记住评价在C秩序和precedence运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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