简体   繁体   English

c中运算符的优先级

[英]precedence of operator in c

I am new to programming. 我是编程新手。 Right now I am trying to learn precedence of operator in C. I try to analyse the code givenbelow. 现在我正试图在C中学习运算符的优先级。我试着分析下面给出的代码。

     #include<stdio.h>
     int main()
     {
        int x, y, z;
        x = y = z= -1;
       z = ++x&&++y&&++z;
       printf("x = %d y = %d z = %d", x, y ,z);
     }

After learning precedence of operator, I understood unary operator has higher preference. 在学习了算子的优先级之后,我理解了一元算子具有更高的偏好。 So in the above code 所以在上面的代码中

 z = ++0&&++0&&++0;

So value of x , y ,z is equal to zero, right? 所以x,y,z的值等于零,对吧? But after compiling I got answer as x = 0, y = -1 and z = 0. 但是在编译之后我得到了答案,因为x = 0,y = -1和z = 0。

Can any one help me to figure out this issue?? 任何人都可以帮我弄清楚这个问题吗?

In evaluating logical AND, If the first expression is zero/false means it wont evaluate the remaining expression. 在评估逻辑AND时,如果第一个表达式为零/ false意味着它不会评估剩余的表达式。 so 所以

 z = ++x&&++y&&++z; // It first increment x, due to pre-increment it becomes zero. 
 // so it wont evaluate the remaining expression in that equation due to Logical AND. it returns 0. (x=0,y=-1,z=-1)
 // but you are assigning return value 0 to z
 z=0;

Try the following code snippets- 请尝试以下代码段 -

   int x, y, z,temp;
   x = y = z= -1;
   temp = ++x&&++y&&++z;
   printf("x = %d y = %d z = %d temp= %d", x, y ,z,temp); // Output 0,-1,-1,0
   x = y = z= -1;
   temp = ++x || ++y&&++z; // But in Logical OR if first expression is true means it wont execute the remaining expression
   printf("x = %d y = %d z = %d temp= %d", x, y ,z,temp); // Output 0,0,-1,0
   x = y = z= -1;
   temp = ++x || ++y || ++z;
   printf("x = %d y = %d z = %d temp= %d", x, y ,z,temp); // Output 0,0,0,0

This expression 这个表达

z = ++x&&++y&&++z;

is equivalent to the following expression 等同于以下表达式

z = ( ++x && ++y ) && ++z;

According to the C Standard 根据C标准

4 Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; 4与按位二进制和运算符不同,&&运算符保证从左到右的评估; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. 如果计算第二个操作数,则在第一个和第二个操作数的计算之间存在一个序列点。 If the first operand compares equal to 0, the second operand is not evaluated. 如果第一个操作数比较等于0,则不计算第二个操作数。

So at first ++x is evaluated. 所以首先评估++x It will be equal to 0. So ++y will not be evaluated. 它将等于0.所以++y将不会被评估。 Expression 表达

( ++x && ++y )

will be equal to 0. As it is equal to 0 then sub-expression ++z in expression ( ++x && ++y ) && ++z will not be evaluated. 将等于0.因为它等于0,所以不会计算表达式( ++x && ++y ) && ++z子表达式++z

Thus z will be assigned the value of the full expression that is equal to 0. 因此, z将被赋予等于0的完整表达式的值。

There is no any undefined behaviour at least because expression ++z will not be evaluated. 至少没有任何未定义的行为,因为不会评估表达式++z

So you will get x == 0, y == -1, and z == 0 (due to the assignment operator). 因此,您将获得x == 0,y == -1和z == 0(由于赋值运算符)。

Logical AND && and Logical OR || 逻辑AND &&和逻辑OR || are evaluated from left to right. 从左到右进行评估。

so in the evaluation of the Expression 所以在表达的评价中

z = ++x&&++y&&++z;//it evaluates ++x and it becomes 0 so next ++y and ++z are not evaluated so now before assignment x=0,y=-1 and z=-1 and z becomes 0 after assignment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM