什么是 C 运算符优先级和结合性?

首先,让我们了解什么是 C 编程语言中的运算符优先级。

运算符优先级

运算符优先级用于计算在表达式中计算的运算符的顺序。在 C 编程中,每个运算符都有一个优先级。当给定表达式中存在多个运算符时,优先级或优先级较高的运算符将首先计算,优先级最低的运算符稍后计算。

运算符关联性

运算符结合性用于计算表达式中具有相同优先级的运算符的顺序。在 C 编程语言中,当一个表达式包含多个具有相等或相同优先级的运算符时,我们使用结合性来确定运算符的计算顺序。

运算符及其优先级和结合性在下表中进行了解释 -

优先级运算符运算符含义关联性
1()
[]
->

Function Call
array reference
Structure member access
Structure member access
左到右
2
~
+
-
++
--
&
*
sizeof(type)
negation
1's complement
Unary Plus
Unary minus
Increment operator
decrement operator
address of operator
pointer
returns size of a variable type conversion
右到左
3*
/
%
multiplication
division
remainder
左到右
4+
-
addition
subtraction
左到右
5<<
>>
left shift
right shift
左到右
6<
<=
less than
less than or equal to
左到右
7==
!=
equal to
not equal to
左到右
8&bitwise AND左到右
9^bitwise EXCLUSIVE OR左到右
10|bitwise OR左到右
11&&logical AND左到右
12||logical OR左到右
13?:Conditional Operator左到右
14=
*=
/=
%=
+=
-=
&=
^=
|=
<<=
>>=
assignment
assign multiplication
assign division
assign remainder
assign addition
assign subtraction
assign bitwise AND
assign bitwise XOR
assign bitwise OR
assign left shift
assign right shif
右到左
15
separator左到右