C++ 运算符优先级列表

C++ Operator Precedence

 

原文地址:http://www.cppblog.com/aqazero/archive/2006/06/08/8284.html

 

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

PrecedenceOperatorDescriptionAssociativity
1::Scope resolutionLeft-to-right
2++ --Suffix/postfix increment and decrement
()Function call
[]Array subscripting
.Element selection by reference
->Element selection through pointer
typeid()Run-time type information (see typeid)
const_castType cast (see const_cast)
dynamic_castType cast (see dynamic_cast)
reinterpret_castType cast (see reinterpret_cast)
static_castType cast (see static_cast)
3++ --Prefix increment and decrementRight-to-left
+ -Unary plus and minus
! ~Logical NOT and bitwise NOT
(type)Type cast
*Indirection (dereference)
&Address-of
sizeofSize-of
new, new[]Dynamic memory allocation
delete, delete[]Dynamic memory deallocation
4.* ->*Pointer to memberLeft-to-right
5* / %Multiplication, division, and remainder/modulus[1]
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 conditionalRight-to-Left
16=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
17throwThrow operator (exceptions throwing)
18,CommaLeft-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 expression std::cout<<a&b is parsed as (std::cout<<a)&b and not std::cout<<(a&b).

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 expressions *p++ and a=b=c are parsed as *(p++) and a=(b=c), and not as (*p)++ or (a=b)=c because of right-to-left associativity.

An operator's precedence is unaffected by overloading.

  1. ISO/IEC 14882:2003 : Programming languages -- C++. "the binary % operator yields the remainder from the division of the first expression by the second. .... If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined".

[edit] See also

Order of evaluation of operator arguments at run time.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,运算符有不同的优先级。根据引用和引用的信息,C语言中运算符优先级从高到低分为以下几个等级: 一级:括号()、[]、对象选择->、. 二级:!非、强制类型转换(int)、自增自减–、取值*、取址&、取反-、取长度sizeof() 三级:算术运算符——乘除余% 加减 四级:左移<<、右移>>运算符 五级:比较运算符:>、>=、<、<=、== 六级:普通逻辑运算符(双目运算符):&、|、&&、|| 七级:三目运算符:?: 八级:赋值运算符=;(/=,*=,%=, =,-=) 九级:逗号运算符。 此外,根据引用的信息,还有一些操作符是从左向右结合的,包括函数调用、数组下标、取成员、类型转换、后置运算符等。 综上所述,这些是C语言中运算符优先级和结合性规则。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++运算符优先级](https://blog.csdn.net/ITJAVAtao/article/details/120597987)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [c++运算符优先级归纳](https://blog.csdn.net/zhaominyong/article/details/126268983)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值