C++ 运算符优先级列表

http://en.cppreference.com/w/cpp/language/operator_precedence

The following table lists the precedence and associativity of C++ operators.
Operators are listed top to bottom, in descending precedence.

Precedence Operator Description Associativity
1 :: Scope resolution Left-to-right
2 ++   -- Suffix/postfix increment and decrement
() Function call
[] Array subscripting
. Element selection by reference
−> Element selection through pointer
3 ++   -- Prefix increment and decrement Right-to-left
+    Unary plus and minus
!   ~ Logical NOT and bitwise NOT
(type) Type cast
* Indirection (dereference)
& Address-of
sizeof Size-of
newnew[] Dynamic memory allocation
deletedelete[] Dynamic memory deallocation
4 .*   ->* Pointer to member Left-to-right
5 *   /   % Multiplication, division, and remainder
6 +    Addition and subtraction
7 <<   >> Bitwise left shift and right shift
8 <   <= For relational operators < and ≤ respectively
>   >= For relational operators > and ≥ respectively
9 ==   != For relational = and ≠ respectively
10 & Bitwise AND
11 ^ Bitwise XOR (exclusive or)
12 | Bitwise OR (inclusive or)
13 && Logical AND
14 || Logical OR
15 ?: Ternary conditional Right-to-left
= Direct assignment (provided by default for C++ classes)
+=   −= 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
16 throw Throw operator (for exceptions)
17 , Comma Left-to-right

When parsing an expression, an operator which is listed on some row
will be bound tighter (as if by parentheses) to its arguments than
any operator that is listed on a row further below it. For example,

the expressions std::cout<<a&b is parsed as (std::cout<<a)&b, not asstd::cout<<(a&b).
the expressions *p++ is parsed as *(p++), not (*p)++.

Operators that are in the same cell (there may be several rows of operators listed in a cell)
are evaluated with the same precedence, in the given direction. For example,
the expression a=b=c is parsed as a=(b=c), and not as (a=b)=c 
because of right-to-left associativity.

An operator's precedence is unaffected by overloading. 

C语言中,运算符的运算优先级共分为15级。1级最高,15级最低。

在表达式中,优先级较高的先于优先级较低的进行运算。

而在一个运算量两侧的运算符优先级相同时, 则按运算符的结合性所规定的结合方向处理。

C语言中各运算符的结合性分为两种,即左结合性(自左至右)和右结合性(自右至左)。

例如算术运算符的结合性是自左至右,即先左后右。
如有表达式x-y+z则y应先与“-”号结合, 执行x-y运算,然后再执行+z的运算。
这种自左至右的结合方向就称为“左结合性”。

而自右至左的结合方向称为“右结合性”。
最典型的右结合性运算符是赋值运算符。
如x=y=z,由于“=”的右结合性,应先执行y=z再执行x=(y=z)运算。

 

 

 

你可能感兴趣的:(C++)