注:文章转载于网络。
C优先级列表
Precedence | Operator | Description | Example | Associativity |
1 |
() |
Grouping operator |
(a + b) / 4; |
left to right |
2 |
! |
Logical negation |
if( !done ) ... |
right to left |
3 |
->* |
Member pointer selector |
ptr->*var = 24; |
left to right |
4 |
* |
Multiplication |
int i = 2 * 4; |
left to right |
5 |
+ |
Addition |
int i = 2 + 3; |
left to right |
6 |
<< |
Bitwise shift left |
int flags = 33 << 1; |
left to right |
7 |
< |
Comparison less-than |
if( i < 42 ) ... |
left to right |
8 |
== |
Comparison equal-to |
if( i == 42 ) ... |
left to right |
9 |
& |
Bitwise AND |
flags = flags & 42; |
left to right |
10 |
^ |
Bitwise exclusive OR |
flags = flags ^ 42; |
left to right |
11 |
| |
Bitwise inclusive (normal) OR |
flags = flags | 42; |
left to right |
12 |
&& |
Logical AND |
if( conditionA && conditionB ) ... |
left to right |
13 |
|| |
Logical OR |
if( conditionA || conditionB ) ... |
left to right |
14 |
? : |
Ternary conditional (if-then-else) |
int i = (a > b) ? a : b; |
right to left |
15 |
= |
Assignment operator |
int a = b; |
right to left |
16 |
, |
Sequential evaluation operator |
for( i = 0, j = 0; i < 10; i++, j++ ) ... |
left to right |
C++优先级列表
Precedence | Operator | Description | Example | Associativity |
1 |
() |
Grouping operator |
(a + b) / 4; |
left to right |
2 |
! |
Logical negation |
if( !done ) ... |
right to left |
3 |
->* |
Member pointer selector |
ptr->*var = 24; |
left to right |
4 |
* |
Multiplication |
int i = 2 * 4; |
left to right |
5 |
+ |
Addition |
int i = 2 + 3; |
left to right |
6 |
<< |
Bitwise shift left |
int flags = 33 << 1; |
left to right |
7 |
< |
Comparison less-than |
if( i < 42 ) ... |
left to right |
8 |
== |
Comparison equal-to |
if( i == 42 ) ... |
left to right |
9 | & | Bitwise AND | flags = flags & 42; | left to right |
10 | ^ | Bitwise exclusive OR | flags = flags ^ 42; | left to right |
11 | | | Bitwise inclusive (normal) OR | flags = flags | 42; | left to right |
12 | && | Logical AND | if( conditionA && conditionB ) ... | left to right |
13 | || | Logical OR | int i = (a > b) ? a : b | left to right |
14 | ? : | Ternary conditional (if-then-else) | if( conditionA || conditionB ) ... | right to left |
15 |
= |
Assignment operator |
int a = b; |
right to left |
16 | , | Sequential evaluation operator | for( i = 0, j = 0; i < 10; i++, j++ ) ... | left to right |