C/C++运算符优先级补记

Ervin posted @ 2013年9月20日 15:31 in 基础知识 with tags c c++ 基础 , 1816 阅读

虽然是非常基础的知识,但还是重新总结一下,免得哪天掉坑里。

优先级的列表就不再贴了,wikipedia上总结的很好了。但是若仔细搜索一下,会发现能找到多个不同的版本,显然有些是错的。

下面是需要注意的点:

1.前置自增/自减和后置自增自减的优先级是不同的,有人给出的优先级列表就犯了这种错误。后置自增自减的优先级更高。

2.位运算的&  ^  |三者优先级不同,且都低于比较运算符。逻辑运算符&&比||优先级高,这几点很坑,为了防止混淆,用的时候最好加括号。

3.逗号运算符的优先级最低,除去::(反正它基本不会产生混淆),[] . ->的优先级位于第一梯队。

 

Avatar_small
Alyssa 说:
2022年12月31日 06:14

Operators in the C and C++ programming languages have a defined priority, which dictates the order in which they are evaluated in an expression. For example, the multiplication operator has a higher priority than the addition operator, so an expression cbd products like "2 + 3 * 4" would be evaluated as "14", not "20". Operator priority can be overridden by explicitly specifying the order of evaluation using parentheses. So, in the previous example, if we wanted the expression to be evaluated as "20", we would write it as "2 + (3 * 4)". There are a few other things to keep in mind when working with operator priority in C and C++. First, all operators have the same priority


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter