小记
虽然自己不会写那些优雅紧凑的C代码,但是偶尔看一些代码(比如GNU项目的源代码和Linux内核源代码)时,还是需要仔细查阅和分析运算符优先级的。
Precedence | Operator | Description | Example | Associativity |
---|---|---|---|---|
1 | () | Groupingoperator | (a + b) /4; | left toright |
2 | ! | Logicalnegation | if( !done )... | right toleft |
3 | ->* | Memberpointer selector | ptr->*var= 24; | left toright |
4 | * | Multiplication | int i = 2 *4; | left toright |
5 | + | Addition | int i = 2 +3; | left toright |
6 | << | Bitwiseshift left | int flags =33 << 1; | left toright |
7 | < | Comparisonless-than | if( i <42 ) ... | left toright |
8 | == | Comparisonequal-to | if( i == 42) ... | left toright |
9 | & | Bitwise AND | flags =flags & 42; | left toright |
10 | ^ | Bitwiseexclusive OR | flags =flags ^ 42; | left toright |
11 | | | Bitwiseinclusive (normal) OR | flags =flags | 42; | left toright |
12 | && | Logical AND | if(conditionA && conditionB ) ... | left toright |
13 | || | Logical OR | if(conditionA || conditionB ) ... | left toright |
14 | ? : | Ternaryconditional (if-then-else) | int i = (a> b) ? a : b; | right toleft |
15 | = | Assignmentoperator | int a =b; | right toleft |
16 | , | Sequentialevaluation operator | for( i = 0,j = 0; i < 10; i++, j++ ) ... | left toright |
阔框点箭域先锋 ① () [] . -> ::
塞外干旱烦政府 ②sizeof ! ~ + -
家家户户限制米 ++ -- (type型) &(取址) *
救灾捐米一点米 ③->* .*
时余曾促物价减 ④% * / ⑤+ -
左邻右舍来相告 ⑥<< >>
老大小凳换大凳 ⑦> < <= >=
把事说明等不等 ⑧== !=
久雨适宜事宜否 ⑨&⑩^⑾|
十二雨下十三过 ⑿&&⒀||
三梦睡醒灯都灭 ⒁:?(三目运算) ⒂= +=...⒃,(逗号)
老实说,我觉的将运算符优先级背下来实在是太坑爹了,想用的时候多用用括号就行了,在此挖苦一下中国式的程序语言教学,把时间浪费在研究诸如 *--p++-- - --q++之类的东西上。
转自:http://www.cppblog.com/aqazero/archive/2006/06/08/8284.html