資料結構中,我們討論了「前序、中序、後序」的表達式。為了能夠由中序,轉前序或後續,我們必須知道運算元的優先度,才能夠完整的用一次掃描(scan)運算式,就得出結果。
所謂結合性,意味著如果有一連串相同的運算,例如說 2*3*4
到底是 (2*3)*4
還是 2*(3*4)
。在C語言都有明確的定義,左結合就是前者,右結合就是後者。
C語言所定義的運算元優限度。
優先級 | 運算元 | 結合性 |
1 | ++ -- Suffix/postfix increment and decrement () Function call[] Array subscripting . Structure and union member access -> Structure and union member access through pointer (type){list} Compound literal(C99) | 左結合 |
2 | ++ -- Prefix increment and decrement+ - Unary plus and minus ! ~ Logical NOT and bitwise NOT (type) Type cast * Indirection (dereference) & Address-of sizeof Size-of_Alignof Alignment requirement(C11) | 右結合 |
3 | * / % Multiplication, division, and remainder | 左結合 |
4 | + - Addition and subtraction | 左結合 |
5 | << >> Bitwise left shift and right shift | 左結合 |
6 | < <= For relational operators < and ≤ respectively > >= For relational operators > and ≥ respectively | 左結合 |
7 | == != For relational = and ≠ respectively | 左結合 |
8 | & Bitwise AND | 左結合 |
9 | ^ Bitwise XOR (exclusive or) | 左結合 |
10 | | Bitwise OR (inclusive or) | 左結合 |
11 | && Logical AND | 左結合 |
12 | || Logical OR | 左結合 |
13 | ?: Ternary conditional | 右結合 |
14 | = Simple assignment += -= Assignment by sum and difference *= /= %= Assignment by product, quotient, and remainder <<= >>= Assignment by bitwise left shift and right shift &= ^= |= Assignment by bitwise AND, XOR, and OR | 右結合 |
15 | , Comma | 左結合 |
依照功能性分類
assignment | increment decrement | arithmetic | logical | comparison | member access | other |
a = b a += b a -= b a *= b a /= b a %= b a &= b a |= b a ^= b a <<= b a >>= b |
++a –a a++ a– |
+a -a a + b a – b a * b a / b a % b ~a a & b a | b a ^ b a << b a >> b |
!a a && b a || b |
a == b a != b a < b a > b a <= b a >= b |
a[b] *a &a a->b a.b |
a(…) a, b (type) a ? : sizeof _Alignof (since C11 |
留言