C語言
優先權 | 運算符 | 名稱或含義 | 使用形式 | 結合方向 | 說明 |
1 | [] | 數組下標 | 數組名[整型表達式] | 左到右 | |
() | 圓括弧 | (表達式)/函式名(形參表) | |||
. | 成員選擇(對象) | 對象.成員名 | |||
-> | 成員選擇(指針) | 對象指針->成員名 | |||
2 | - | 負號運算符 | -表達式 | 右到左 | 單目運算符 |
(類型) | 強制類型轉換 | (數據類型)表達式 | |||
++ | 自增運算符 | ++變數名/變數名++ | 單目運算符 | ||
-- | 自減運算符 | --變數名/變數名-- | 單目運算符 | ||
* | 取值運算符 | *指針表達式 | 單目運算符 | ||
& | 取地址運算符 | &左值表達式 | 單目運算符 | ||
! | 邏輯非運算符 | !表達式 | 單目運算符 | ||
~ | 按位取反運算符 | ~表達式 | 單目運算符 | ||
sizeof | 長度運算符 | sizeof 表達式/sizeof(類型) | |||
3 | / | 除 | 表達式/表達式 | 左到右 | 雙目運算符 |
* | 乘 | 表達式*表達式 | 雙目運算符 | ||
% | 餘數(取模) | 整型表達式%整型表達式 | 雙目運算符 | ||
4 | + | 加 | 表達式+表達式 | 左到右 | 雙目運算符 |
- | 減 | 表達式-表達式 | 雙目運算符 | ||
5 | << | 左移 | 表達式<<表達式 | 左到右 | 雙目運算符 |
>> | 右移 | 表達式>>表達式 | 雙目運算符 | ||
6 | > | 大於 | 表達式>表達式 | 左到右 | 雙目運算符 |
>= | 大於等於 | 表達式>=表達式 | 雙目運算符 | ||
< | 小於 | 表達式<表達式 | 雙目運算符 | ||
<= | 小於等於 | 表達式<=表達式 | 雙目運算符 | ||
7 | == | 等於 | 表達式==表達式 | 左到右 | 雙目運算符 |
!= | 不等於 | 表達式!= 表達式 | 雙目運算符 | ||
8 | & | 按位與 | 整型表達式&整型表達式 | 左到右 | 雙目運算符 |
9 | ^ | 按位異或 | 整型表達式^整型表達式 | 左到右 | 雙目運算符 |
10 | | | 按位或 | 整型表達式|整型表達式 | 左到右 | 雙目運算符 |
11 | && | 邏輯與 | 表達式&&表達式 | 左到右 | 雙目運算符 |
12 | || | 邏輯或 | 表達式||表達式 | 左到右 | 雙目運算符 |
13 | ?: | 條件運算符 | 表達式1? 表達式2: 表達式3 | 右到左 | 三目運算符 |
14 | = | 賦值運算符 | 變數=表達式 | 右到左 | |
/= | 除後賦值 | 變數/=表達式 | |||
*= | 乘後賦值 | 變數*=表達式 | |||
%= | 取模後賦值 | 變數%=表達式 | |||
+= | 加後賦值 | 變數+=表達式 | |||
-= | 減後賦值 | 變數-=表達式 | |||
<<= | 左移後賦值 | 變數<<=表達式 | |||
>>= | 右移後賦值 | 變數>>=表達式 | |||
&= | 按位與後賦值 | 變數&=表達式 | |||
^= | 按位異或後賦值 | 變數^=表達式 | |||
|= | 按位或後賦值 | 變數|=表達式 | |||
15 | , | 逗號運算符 | 表達式,表達式,… | 左到右 | 從左向右順序運算 |
以下部分錯誤:
結合性 | |
---|---|
() [] -> . ++(後綴自增) --(後綴自減) | left to right |
! ~ ++(前綴自增) --(前綴自減) + - * sizeof(type) | right to left |
* / % | left to right |
+ - | |
<< >> | |
< <= > >= | |
== != | |
& | |
^ | |
| | |
&& | |
|| | |
? : (條件運算) | right to left |
= += -= *= /= %= &= ^= |= <<= >>= | |
left to right |
接下來是算術運算符,*、/、%的優先權當然比+、-高了。
移位運算符緊隨其後。
其次的關係運算符中,< <= > >=要比 == !=高一個級別,不大好理解。
所有的邏輯操作符都具有不同的優先權(單目運算符除外,!和~)
邏輯位操作符的"與"比"或"高,而"異或"則在它們之間。
跟在其後的&&比||高。
接下來的是條件運算符,賦值運算符及逗號運算符。
在C語言中,只有4個運算符規定了運算方向,它們是&&、| |、條件運算符及逗號運算符。
&&、| |都是先計算左邊表達式的值,當左邊表達式的值能確定整個表達式的值時,就不再計算右邊表達式的值。如 a = 0 && b; &&運算符的左邊位為0,則右邊表達式b就不再判斷。
在條件運算符中。如a?b:c;先判斷a的值,再根據a的值對b或c之中的一個進行求值。
逗號表達式則規定從左到右依次進行求值。
C++
Operator | Description | Example | Overloadable |
Group 1 (no associativity) | |||
:: | Scope resolution operator | Class::age = 2; | NO |
Group 2 | |||
() | Function call | isdigit('1') | YES |
() | Member initalization | c_tor(int x, int y) : _x(x), _y(y*10){}; | YES |
[] | Array access | array[4] = 2; | YES |
-> | Member access from a pointer | ptr->age = 34; | YES |
. | Member access from an object | obj.age = 34; | NO |
++ | Post-increment | for( int i = 0; i < 10; i++ ) cout << i; | YES |
-- | Post-decrement | for( int i = 10; i > 0; i-- ) cout << i; | YES |
const_cast | Special cast | const_cast<type_to>(type_from); | NO |
dynamic_cast | Special cast | dynamic_cast<type_to>(type_from); | NO |
static_cast | Special cast | static_cast<type_to>(type_from); | NO |
reinterpret_cast | Special cast | reinterpret_cast<type_to>(type_from); | NO |
typeid | Runtime type information | cout « typeid(var).name(); cout « typeid(type).name(); | NO |
Group 3 (right-to-left associativity) | |||
! | Logical negation | if( !done ) … | YES |
not | Alternate spelling for ! | ||
~ | Bitwise complement | flags = ~flags; | YES |
compl | Alternate spelling for ~ | ||
++ | Pre-increment | for( i = 0; i < 10; ++i ) cout << i; | YES |
-- | Pre-decrement | for( i = 10; i > 0; --i ) cout << i; | YES |
- | Unary minus | int i = -1; | YES |
+ | Unary plus | int i = +1; | YES |
* | Dereference | int data = *intPtr; | YES |
& | Address of | int *intPtr = &data; | YES |
new | Dynamic memory allocation | long *pVar = new long; MyClass *ptr = new MyClass(args); | YES |
new [] | Dynamic memory allocation of array | long *array = new long[n]; | YES |
delete | Deallocating the memory | delete pVar; | YES |
delete [] | Deallocating the memory of array | delete [] array; | YES |
(type) | Cast to a given type | int i = (int) floatNum; | YES |
sizeof | Return size of an object or type | int size = sizeof floatNum; int size = sizeof(float); | NO |
Group 4 | |||
->* | Member pointer selector | ptr->*var = 24; | YES |
.* | Member object selector | obj.*var = 24; | NO |
Group 5 | |||
* | Multiplication | int i = 2 * 4; | YES |
/ | Division | float f = 10.0 / 3.0; | YES |
% | Modulus | int rem = 4 % 3; | YES |
Group 6 | |||
+ | Addition | int i = 2 + 3; | YES |
- | Subtraction | int i = 5 - 1; | YES |
Group 7 | |||
<< | Bitwise shift left | int flags = 33 << 1; | YES |
>> | Bitwise shift right | int flags = 33 >> 1; | YES |
Group 8 | |||
< | Comparison less-than | if( i < 42 ) … | YES |
<= | Comparison less-than-or-equal-to | if( i <= 42 ) ... | YES |
> | Comparison greater-than | if( i > 42 ) … | YES |
>= | Comparison greater-than-or-equal-to | if( i >= 42 ) ... | YES |
Group 9 | |||
== | Comparison equal-to | if( i == 42 ) ... | YES |
eq | Alternate spelling for == | ||
!= | Comparison not-equal-to | if( i != 42 ) … | YES |
not_eq | Alternate spelling for != | ||
Group 10 | |||
& | Bitwise AND | flags = flags & 42; | YES |
bitand | Alternate spelling for & | ||
Group 11 | |||
^ | Bitwise exclusive OR (XOR) | flags = flags ^ 42; | YES |
xor | Alternate spelling for ^ | ||
Group 12 | |||
| | Bitwise inclusive (normal) OR | flags = flags | 42; | YES |
bitor | Alternate spelling for | | ||
Group 13 | |||
&& | Logical AND | if( conditionA && conditionB ) … | YES |
and | Alternate spelling for && | ||
Group 14 | |||
|| | Logical OR | if( conditionA || conditionB ) ... | YES |
or | Alternate spelling for || | ||
Group 15 (right-to-left associativity) | |||
? : | Ternary conditional (if-then-else) | int i = (a > b) ? a : b; | NO |
Group 16 (right-to-left associativity) | |||
= | Assignment operator | int a = b; | YES |
+= | Increment and assign | a += 3; | YES |
-= | Decrement and assign | b -= 4; | YES |
*= | Multiply and assign | a *= 5; | YES |
/= | Divide and assign | a /= 2; | YES |
%= | Modulo and assign | a %= 3; | YES |
&= | Bitwise AND and assign | flags &= new_flags; | YES |
and_eq | Alternate spelling for &= | ||
^= | Bitwise exclusive or (XOR) and assign | flags ^= new_flags; | YES |
xor_eq | Alternate spelling for ^= | ||
|= | Bitwise normal OR and assign | flags |= new_flags; | YES |
or_eq | Alternate spelling for |= | ||
<<= | Bitwise shift left and assign | flags <<= 2; | YES |
>>= | Bitwise shift right and assign | flags >>= 2; | YES |
Group 17 | |||
throw | throw exception | throw EClass(“Message”); | NO |
Group 18 | |||
, | Sequential evaluation operator | for( i = 0, j = 0; i < 10; i++, j++ ) … | YES |
C#
優先權 | 類別 | 運算符 |
1 | 基本 | (x) x.y f(x) a[x] x++ x――new typeof sizeof checked unchecked |
2 | 單目 | + - ! ~ ++x ――x (T)x |
3 | 乘法與除法 | * / % |
4 | 加法與減法 | + - |
5 | 移位運算 | << >> |
6 | 關係運算 | < > < = >= |
7 | 條件等 | = = ! = |
8 | 位邏輯與 | & |
9 | 位邏輯異或 | ^ |
10 | 位邏輯或 | | |
11 | 條件與 | && |
12 | 條件或 | ‖ |
13 | 條件 | ?: |
14 | 賦值 | = *= /= %= += -= <<= >>= &= ^= |= |