技术 > 运算符优先级
目录
No. 1
  在某个表达式中出现多种运算时,将按照预先确定的顺序(称作“运算符优先级”)计算并解析各个部分。
  当表达式包含不止一种运算符时,则按照下列规则对其进行计算。
  算术运算符和串联运算符具有下述优先级顺序,它们的优先级均高于比较运算符、逻辑运算符和位运算符。
  比较运算符具有相同的优先顺序,它们的优先级均高于逻辑运算符和位运算符,但低于算术运算符和串联运算符。
  逻辑运算符和位运算符具有下述优先顺序,它们的优先级均低于算术运算符、串联运算符和比较运算符。
  具有相同优先顺序的运算符将按照它们在表达式中出现的顺序从左至右进行计算。
  运算符的计算优先级顺序如下:
  算术运算符和串联运算符
  求幂 (^)
  一元求反 (╟)
  乘法和除法(*、/)
  整数除法 ()
  取模 (mod)
  加法和减法(+、-),字符串串联 (+)
  字符串连接 (&)
  数学移位(<<、>>)
  比较运算符
  所有比较运算符(=、<>、<、<=、>、>=、like、is、typeof...is)
  逻辑运算符和位运算符
  非 (not)
  逻辑合取(and、andalso)
  逻辑析取(or、orelse、xor)
  字符串串联运算符 (&) 不是算术运算符,但它在优先级方面与算术运算符属于一组。
  is 运算符是一个对象引用比较运算符。它不比较对象或它们的值;它只是进行检查以确定两个对象变量是否指的是相同的对象。
  当具有相同优先顺序的运算符(例如乘法和除法)在表达式中一起出现时,每个运算符将按其出现的顺序从左至右进行计算。可以使用括号来改写优先顺序,以强制优先计算表达式的某些部分。括号内的运算总比括号外的运算先执行。但是在括号内,运算符优先级保持不变。
No. 2
  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 &laquo; typeid(var).name();
  cout &laquo; 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