Cprogramming 简明教程

Operator Precedence in C

C 中的单个表达式可能具有类型不同的多个运算符。C 编译器根据运算符的优先级和结合性计算其值。

A single expression in C may have multiple operators of different types. The C compiler evaluates its value based on the operator precedence and associativity of operators.

运算符的优先级决定了在表达式中计算它们时的顺序。优先级较高的运算符优先计算。

The precedence of operators determines the order in which they are evaluated in an expression. Operators with higher precedence are evaluated first.

例如,请看此表达式 -

For example, take a look at this expression −

x = 7 + 3 * 2;

此处,乘法运算符“*”的优先级高于加法运算符“+”。因此,首先执行乘法 3*2,然后将其添加到 7 中,得出“x = 13”。

Here, the multiplication operator "*" has a higher precedence than the addition operator "+". So, the multiplication 3*2 is performed first and then adds into 7, resulting in "x = 13".

下表列出了 C 中运算符优先级的顺序。此处,优先级最高的运算符出现在表格顶部,而优先级最低的运算符出现在底部。

The following table lists the order of precedence of operators in C. Here, operators with the highest precedence appear at the top of the table, and those with the lowest appear at the bottom.

Category

Operator

Associativity

Postfix

() [] → . ++ - -

Left to right

Unary

+ - ! ~ ++ - - (type)* & sizeof

Right to left

Multiplicative

* / %

Left to right

Additive

+ -

Left to right

Shift

<< >>

Left to right

Relational

< ⇐ > >=

Left to right

Equality

== !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

Left to right

Logical AND

&&

Left to right

Logical OR

Left to right

Conditional

?:

Right to left

Assignment

= += -= *= /= %⇒>= <⇐ &= ^=

=

Right to left

Comma

,

在表达式中,优先级较高的运算符将首先计算。

Within an expression, higher precedence operators will be evaluated first.

Operator Associativity

在 C 中,运算符的结合性是指在程序中计算运算符的表达式(从左到右或从右到左)。当一个表达式中出现两个优先级相同的运算符时,使用运算符结合性。

In C, the associativity of operators refers to the direction (left to right or right to left) an expression is evaluated within a program. Operator associativity is used when two operators of the same precedence appear in an expression.

在以下示例中 -

In the following example −

15 / 5 * 2

“/”(除法)和“*”(乘法)运算符都具有相同的优先级,因此求值顺序将由结合性决定。

Both the "/" (division) and "*" (multiplication) operators have the same precedence, so the order of evaluation will be decided by associativity.

根据上述表格,乘法运算符的结合性是从左到右。因此,表达式的计算结果为 -

As per the above table, the associativity of the multiplicative operators is from Left to Right. So, the expression is evaluated as −

(15 / 5) * 2

计算结果为 -

It evaluates to −

3 * 2 = 6

Example 1

在以下代码中,乘法和除法运算符的优先级高于加法运算符。

In the following code, the multiplication and division operators have higher precedence than the addition operator.

乘法运算符的左到右结合性导致“ b ”和“ c ”相乘,除以“ e ”。然后将结果添加到“ a ”的值中。

The left−to−right associativity of multiplicative operator results in multiplication of "b" and "c" divided by "e". The result then adds up to the value of "a".

#include <stdio.h>

int main(){

   int a = 20;
   int b = 10;
   int c = 15;
   int d = 5;

   int e;
   e = a + b * c / d;
   printf("e : %d\n" ,  e );

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

e: 50

Example 2

我们可使用括号来更改求值顺序。括号()在所有 C 运算符中具有最高优先级。

We can use parenthesis to change the order of evaluation. Parenthesis () got the highest priority among all the C operators.

#include <stdio.h>

int main(){

   int a = 20;
   int b = 10;
   int c = 15;
   int d = 5;
   int e;

   e = (a + b) * c / d;
   printf("e:  %d\n",  e);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

e: 90

在此表达式中,首先在括号中添加 a 和 b。将结果乘以 c,然后进行除以 d 的运算。

In this expression, the addition of a and b in parenthesis is first. The result is multiplied by c and then the division by d takes place.

Example 3

在计算 e 的表达式中,我们已将 a+b 放在一个括号内,将 c/d 放在另一个括号内,将两者相乘。

In the expression that calculates e, we have placed a+b in one parenthesis, and c/d in another, multiplying the result of the two.

#include <stdio.h>

int main(){

   int a = 20;
   int b = 10;
   int c = 15;
   int d = 5;

   int e;
   e = (a + b) * (c / d);
   printf("e: %d\n",  e );

   return 0;
}

运行此代码,您将获得以下输出−

On running this code, you will get the following output −

e: 90

Precedence of Post / Prefix Increment / Decrement Operators

“”和“− −”运算符分别作为增量和减量运算符。“”是单目运算符,可以用作变量的前缀或后缀。

The "++" and "− −" operators act as increment and decrement operators, respectively. They are unary in nature and can be used as a prefix or postfix to a variable.

当独立使用时,在中缀或后缀方式中使用这些运算符具有相同的效果。换句话说,“a”和“a”具有相同的效果。然而,当这些“++”或“− −”运算符与表达式中的其他运算符一起出现时,它们的行为就不同。

When used as a standalone, using these operators in a prefix or post−fix manner has the same effect. In other words, "a" has the same effect as "a". However, when these "++" or "− −" operators appear along with other operators in an expression, they behave differently.

后缀增量和减量运算符的优先级高于前缀增量和减量运算符。

Postfix increment and decrement operators have higher precedence than prefix increment and decrement operators.

Example

以下示例显示了如何在 C 程序中使用增量和减量运算符−

The following example shows how you can use the increment and decrement operators in a C program −

#include <stdio.h>

int main(){

   int x = 5, y = 5, z;
   printf("x: %d \n", x);

   z = x++;
   printf("Postfix increment: x: %d z: %d\n", x, z);

   z = ++y;
   printf("Prefix increment. y: %d z: %d\n", y ,z);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

x: 5
Postfix increment: x: 6 z: 5
Prefix increment. y: 6 z: 6

逻辑运算符具有左至右结合性。然而,编译器计算表达式结果所需的最小数量的运算数。由此,表达式的某些运算数可能不会被计算。

Logical operators have left−to−right associativity. However, the compiler evaluates the least number of operands needed to determine the result of the expression. As a result, some operands of the expression may not be evaluated.

例如,请注意以下表达式 −

For example, take a look at the following expression −

x > 50 && y > 50

在此处,只有当第一个表达式计算为 True 时,才会计算第二个运算数“y > 50”。

Here the second operand "y > 50" is evaluated only if the first expression evaluates to True.