Ashamed ah, today wrote an algorithm problem, for the first time did not write right. Leng Shimo change for a long time see what was wrong, say about the process back, suddenly found operator precedence angered disaster if (! Num% 2) {......} ,! The operation% higher priority than, ah ah ah, I found a frenzied ten minutes, gave the old head disgrace. In order to avoid such a stupid mistake, so write this blog about the consolidation of operator precedence.
Not a lot to say, directly on the table.
priority
|
Operators
|
name
|
Instructions
|
Order of operations
|
Explanation
|
1
|
[]
|
Array subscript
|
Array name [integer expression]
|
Left to right
|
|
()
|
Parentheses
|
(Expression) / function name (parameter list)
|
|||
.
|
Member selection (objects)
|
Object. Member Name
|
|||
->
|
Member selection (pointer)
|
Object pointer -> member name
|
|||
2
|
-
|
Operators negative
|
- type arithmetic expressions
|
Right to Left
|
Unary operator
|
(type)
|
Cast
|
(Scalar data type) scalar expression
|
|||
++
|
Increment operator
|
++ scalar type can modify lvalue expression
|
Unary operator
|
||
--
|
Decrement operator
|
- scalar type modifiable lvalue expression
|
Unary operator
|
||
*
|
Dereferencing operator
|
* Pointer type expression
|
Unary operator
|
||
&
|
The address operator
|
&expression
|
Unary operator
|
||
!
|
Logical NOT operator
|
! Scalar type expression
|
Unary operator
|
||
~
|
Bitwise operator
|
~ Integer expression
|
Unary operator
|
||
sizeof
|
Operators length
|
sizeof expressions
sizeof (Type)
|
|||
3 |
/
|
except
|
Expressions / Expression
|
Left to right |
Binary operator
|
*
|
Multiply
|
Expression * expression
|
Binary operator
|
||
%
|
The remainder (modulo)
|
Integer expression integer expression%
|
Binary operator
|
||
4
|
+
|
plus
|
Expression + expression
|
Left to right
|
Binary operator
|
-
|
Less
|
Expressions - Expressions
|
Binary operator
|
||
5
|
<<
|
The left
|
Integer expression << integer expression
|
Left to right
|
Binary operator
|
>>
|
Right
|
>> integer expression integer expression
|
Binary operator
|
||
6
|
>
|
more than the
|
Expressions> Expressions
|
Left to right
|
Binary operator
|
>=
|
greater or equal to
|
Expression> = expression
|
Binary operator
|
||
<
|
Less than
|
Expression <expression
|
Binary operator
|
||
<=
|
Less than or equal
|
Expression <= expression
|
Binary operator
|
||
7
|
==
|
equal
|
Expression == expression
|
Left to right
|
Binary operator
|
!=
|
not equal to
|
Expression! = Expression
|
Binary operator
|
||
8
|
&
|
Bitwise AND
|
Integer expression & integer expression
|
Left to right
|
Binary operator
|
9
|
^
|
Bitwise XOR
|
Integer expression ^ integer expression
|
Left to right
|
Binary operator
|
10
|
|
|
Bitwise or
|
Integer expression | integer expression
|
Left to right
|
Binary operator
|
11
|
&&
|
Logic and
|
Expression && expression
|
Left to right
|
Binary operator
|
12
|
||
|
Logical or
|
Expressions Expressions ||
|
Left to right
|
Binary operator
|
13
|
?:
|
Conditional operator
|
Expression 1 Expression 2:? Expression 3
|
Right to Left
|
Ternary operator
|
14
|
=
|
Assignment Operators
|
Modifiable lvalue expression = Expression
|
Right to Left
|
by = |
/=
|
In addition to the assignment
|
Value of the expression may be modified left / Expression =
|
by = | ||
*=
|
Take after the assignment
|
Modifiable lvalue expression * = Expression
|
by = | ||
%=
|
After the assignment modulo
|
Modifiable lvalue expression = Expression%
|
by = | ||
+=
|
After the addition of the assignment
|
Value of the expression may be modified left + = expression
|
by = | ||
-=
|
After subtraction assignment
|
Modifiable lvalue expression - expression =
|
by = | ||
<<=
|
Left after the assignment
|
可修改左值表达式<<=表达式
|
op= | ||
>>=
|
右移后赋值
|
可修改左值表达式>>=表达式
|
op= | ||
&=
|
按位与后赋值
|
可修改左值表达式&=表达式
|
op= | ||
^=
|
按位异或后赋值
|
可修改左值表达式^=表达式
|
op= | ||
|=
|
按位或后赋值
|
可修改左值表达式|=表达式
|
op= |
注意到了吗?并不是所有的逻辑运算符都在算数运算符后面 !这玩意就是特例,而且,逻辑运算符也是有优先级的(离散数学里讲过)所以运算符优先级应该是
! > 算术 > 关系 > && > || > 三目 > 赋值 > op=