c語言的操作符共有15個優先級,如下:
Operators Associativity
() [] -> . left to right
! ~ ++ -- + - * (type) sizeof right to left
* / % left to right
+ - left to right
<< >> left to right
< <= > >= left to right
== != left to right
& left to right
^ left to right
| left to right
&& left to right
|| left to right
?: right to left
= += -= *= /= %= &= ^= |= <<= >>= right to left
, left to right
優先級從上到下依次遞減,最上面具有最高的優先級,逗號操作符具有最低的優先級。
所有的優先級中,衹有三個優先級是從右至左結合的,它們是單目運算符、條件運算符、賦值運算符。其它的都是從左至右結合。
具有最高優先級的其實並不算是真正的運算符,它們算是一類特殊的操作。()是與函數相關,[]與數組相關,而->及.是取結構成員。
其次是單目運算符,所有的單目運算符具有相同的優先級,因此在我認為的 真正的運算符中它們具有最高的優先級,又由於它們都是從右至左結合的,因此*p++與*(p++)等效是毫無疑問的。
接下來是算術運算符,*、/、%的優先級當然比+、-高了。
移位運算符緊隨其後。
其次的關係運算符中,< <= > >=要比 == !=高一個級別,不大好理解。
所有的邏輯操作符都具有不同的優先級(單目運算符出外,!和~)
邏輯位操作符的"與"比"或"高,而"異或"則在它們之間。
跟在其後的&&比||高。
接下來的是條件運算符,賦值運算符及逗號運算符。
在C語言中,衹有4個運算符規定了運算方向,它們是&&、| |、條件運算符及賦值運算符。
&&、| |都是先計算左邊表達式的值,當左邊表達式的值能確定整個表達式的值時,就不再計算右邊表達式的值。如 a = 0 && b; &&運算符的左邊位0,則右邊表達式b就不再判斷。
在條件運算符中。如a?b:c;先判斷a的值,再根據a的值對b或c之中的一個進行求值。
賦值表達式則規定先對右邊的表達式求值,因此使 a = b = c = 6;成為可能。
C++運算符優先級
Operator Description Example Overloadable Group 1 (no associativity) ::Scope resolution operatorClass::age = 2;NOGroup 2 ()Function callisdigit('1')YES()Member initalization c_tor(int x, int y) : _x(x), _y(y*10){};YES[]Array accessarray = 2;YES->Member access from a pointerptr->age = 34;YES.Member access from an objectobj.age = 34;NO++Post-incrementfor( int i = 0; i < 10; i++ ) cout << i;YES--Post-decrementfor( int i = 10; i > 0; i-- ) cout << i;YESconst_castSpecial castconst_cast<type_to>(type_from);NOdynamic_castSpecial castdynamic_cast<type_to>(type_from);NOstatic_castSpecial caststatic_cast<type_to>(type_from);NOreinterpret_castSpecial castreinterpret_cast<type_to>(type_from);NOtypeidRuntime type informationcout « typeid(var).name();
cout « typeid(type).name();NOGroup 3 (right-to-left associativity) !Logical negationif( !done ) …YESnotAlternate spelling for ! ~Bitwise complementflags = ~flags;YEScomplAlternate spelling for ~ ++Pre-incrementfor( i = 0; i < 10; ++i ) cout << i;YES--Pre-decrementfor( i = 10; i > 0; --i ) cout << i;YES-Unary minusint i = -1;YES+Unary plusint i = +1;YES*Dereferenceint data = *intPtr;YES&Address ofint *intPtr = &data;YESnewDynamic memory allocationlong *pVar = new long;
MyClass *ptr = new MyClass(args);YESnew []Dynamic memory allocation of arraylong *array = new long[n];YESdeleteDeallocating the memorydelete pVar;YESdelete []Deallocating the memory of arraydelete [] array;YES(type)Cast to a given typeint i = (int) floatNum;YESsizeofReturn size of an object or typeint size = sizeof floatNum;
int size = sizeof(float);NOGroup 4 ->*Member pointer selectorptr->*var = 24;YES.*Member object selectorobj.*var = 24;NOGroup 5 *Multiplicationint i = 2 * 4;YES/Divisionfloat f = 10.0 / 3.0;YES%Modulusint rem = 4 % 3;YESGroup 6 +Additionint i = 2 + 3;YES-Subtractionint i = 5 - 1;YESGroup 7 <<Bitwise shift leftint flags = 33 << 1;YES>>Bitwise shift rightint flags = 33 >> 1;YESGroup 8 <Comparison less-thanif( i < 42 ) …YES<=Comparison less-than-or-equal-toif( i <= 42 ) ...YES>Comparison greater-thanif( i > 42 ) …YES>=Comparison greater-than-or-equal-toif( i >= 42 ) ...YESGroup 9 ==Comparison equal-toif( i == 42 ) ...YESeqAlternate spelling for == !=Comparison not-equal-toif( i != 42 ) …YESnot_eqAlternate spelling for != Group 10 &Bitwise ANDflags = flags & 42;YESbitandAlternate spelling for & Group 11 ^Bitwise exclusive OR (XOR)flags = flags ^ 42;YESxorAlternate spelling for ^ Group 12 |Bitwise inclusive (normal) ORflags = flags | 42;YESbitorAlternate spelling for | Group 13 &&Logical ANDif( conditionA && conditionB ) …YESandAlternate spelling for && Group 14 ||Logical ORif( conditionA || conditionB ) ...YESorAlternate spelling for || Group 15 (right-to-left associativity) ? :Ternary conditional (if-then-else)int i = (a > b) ? a : b;NOGroup 16 (right-to-left associativity) =Assignment operatorint a = b;YES+=Increment and assigna += 3;YES-=Decrement and assignb -= 4;YES*=Multiply and assigna *= 5;YES/=Divide and assigna /= 2;YES%=Modulo and assigna %= 3;YES&=Bitwise AND and assignflags &= new_flags;YESand_eqAlternate spelling for &= ^=Bitwise exclusive or (XOR) and assignflags ^= new_flags;YESxor_eqAlternate spelling for ^= |=Bitwise normal OR and assignflags |= new_flags;YESor_eqAlternate spelling for |= <<=Bitwise shift left and assignflags <<= 2;YES>>=Bitwise shift right and assignflags >>= 2;YESGroup 17 throwthrow exceptionthrow EClass(“Message”);NOGroup 18 ,Sequential evaluation operatorfor( i = 0, j = 0; i < 10; i++, j++ ) …YES |