博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C和C++操作符优先级顺序
阅读量:6232 次
发布时间:2019-06-21

本文共 4865 字,大约阅读时间需要 16 分钟。

hot3.png

都开始搞程序了,连这种最基本的问题都不是很确定。

C优先级列表

Precedence Operator Description Example Associativity
1

()

[]
->
.
::
++
--

Grouping operator

Array access
Member access from a pointer
Member access from an object
Scoping operator
Post-increment
Post-decrement

(a + b) / 4;

array[4] = 2;
ptr->age = 34;
obj.age = 34;
Class::age = 2;
for( i = 0; i < 10; i++ ) ...
for( i = 10; i > 0; i-- ) ...

 

 

left to right

2

!

~
++
--
-
+
*
&
(type)

Logical negation

Bitwise complement
Pre-increment
Pre-decrement
Unary minus
Unary plus
Dereference
Address of
Cast to a given type
Return size in bytes

if( !done ) ...

flags = ~flags;
for( i = 0; i < 10; ++i ) ...
for( i = 10; i > 0; --i ) ...
int i = -1;
int i = +1;
data = *ptr;
address = &obj;
int i = (int) floatNum;
int size = sizeof(floatNum);

right to left

3

->*

.*

Member pointer selector

Member pointer selector

ptr->*var = 24;

obj.*var = 24;

left to right
4

*

/
%

Multiplication

Division
Modulus

int i = 2 * 4;

float f = 10 / 3;
int rem = 4 % 3;

left to right

5

+

-

Addition

Subtraction

int i = 2 + 3;

int i = 5 - 1;

left to right

6

<<

>>

Bitwise shift left

Bitwise shift right

int flags = 33 << 1;

int flags = 33 >> 1;

left to right

7

<

<=
>
>=

Comparison less-than

Comparison less-than-or-equal-to
Comparison greater-than
Comparison geater-than-or-equal-to

if( i < 42 ) ...

if( i <= 42 ) ...
if( i > 42 ) ...
if( i >= 42 ) ...

 

 

left to right

8

==

!=

Comparison equal-to

Comparison not-equal-to

if( i == 42 ) ...

if( i != 42 ) ...

left to right

9

&

Bitwise AND

flags = flags & 42;

left to right

10

^

Bitwise exclusive OR

flags = flags ^ 42;

left to right

11

|

Bitwise inclusive (normal) OR

flags = flags | 42;

left to right

12

&&

Logical AND

if( conditionA && conditionB ) ...

left to right

13

||

Logical OR

if( conditionA || conditionB ) ...

left to right

14

? :

Ternary conditional (if-then-else)

int i = (a > b) ? a : b;

right to left

15

=

+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=

Assignment operator

Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise AND and assign
Bitwise exclusive OR and assign
Bitwise inclusive (normal) OR and assign
Bitwise shift left and assign
Bitwise shift right and assign

int a = b;

a += 3;
b -= 4;
a *= 5;
a /= 2;
a %= 3;
flags &= new_flags;
flags ^= new_flags;
flags |= new_flags;
flags <<= 2;
flags >>= 2;

 

 

 

 

 

right to left

16

,

Sequential evaluation operator

for( i = 0, j = 0; i < 10; i++, j++ ) ...

left to right

 

C++优先级列表

 

Precedence Operator Description Example Associativity
1

()

[]
->
.
::
++
--

Grouping operator

Array access
Member access from a pointer
Member access from an object
Scoping operator
Post-increment
Post-decrement

(a + b) / 4;

array[4] = 2;
ptr->age = 34;
obj.age = 34;
Class::age = 2;
for( i = 0; i < 10; i++ ) ...
for( i = 10; i > 0; i-- ) ...

left to right
2

!

~
++
--
-
+
*
&
(type)

Logical negation

Bitwise complement
Pre-increment
Pre-decrement
Unary minus
Unary plus
Dereference
Address of
Cast to a given type
Return size in bytes

if( !done ) ...

flags = ~flags;
for( i = 0; i < 10; ++i ) ...
for( i = 10; i > 0; --i ) ...
int i = -1;
int i = +1;
data = *ptr;
address = &obj;
int i = (int) floatNum;
int size = sizeof(floatNum);

right to left
3

->*

.*

Member pointer selector

Member pointer selector

ptr->*var = 24;

obj.*var = 24;

left to right
4

*

/
%

Multiplication

Division
Modulus

int i = 2 * 4;

float f = 10 / 3;
int rem = 4 % 3;

left to right
5

+

-

Addition

Subtraction

int i = 2 + 3;

int i = 5 - 1;

left to right
6

<<

>>

Bitwise shift left

Bitwise shift right

int flags = 33 << 1;

int flags = 33 >> 1;

left to right
7

<

<=
>
>=

Comparison less-than

Comparison less-than-or-equal-to
Comparison greater-than
Comparison geater-than-or-equal-to

if( i < 42 ) ...

if( i <= 42 ) ...
if( i > 42 ) ...
if( i >= 42 ) ...

left to right
8

==

!=

Comparison equal-to

Comparison not-equal-to

if( i == 42 ) ...

if( i != 42 ) ...

left to right
9 & Bitwise AND flags = flags & 42; left to right
10 ^ Bitwise exclusive OR flags = flags ^ 42; left to right
11 | Bitwise inclusive (normal) OR flags = flags | 42; left to right
12 && Logical AND if( conditionA && conditionB ) ... left to right
13 || Logical OR int i = (a > b) ? a : b left to right
14 ? : Ternary conditional (if-then-else) if( conditionA || conditionB ) ... right to left
15

=

+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=

Assignment operator

Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise AND and assign
Bitwise exclusive OR and assign
Bitwise inclusive (normal) OR and assign
Bitwise shift left and assign
Bitwise shift right and assign

int a = b;

a += 3;
b -= 4;
a *= 5;
a /= 2;
a %= 3;
flags &= new_flags;
flags ^= new_flags;
flags |= new_flags;
flags <<= 2;
flags >>= 2

right to left
16 , Sequential evaluation operator for( i = 0, j = 0; i < 10; i++, j++ ) ... left to right

转载于:https://my.oschina.net/Cf438NuwHLl/blog/129666

你可能感兴趣的文章
group by 怎么用java对象接收_生产服务宕机,线上业务挂了!Promtheus 怎么又不报警了呢?...
查看>>
himawari-8卫星叶绿素a产品、_走过50年,看“风云”眼中的世界| 卫星看中国特别版...
查看>>
mybatis使用$报空指针_打破你的认知!Java空指针居然还能这样玩,90%人不知道…...
查看>>
windows mysql 重置root密码_在Windows下Mysql如何重置root用户密码
查看>>
mysql5.6 linux下载_mysql-5.6.45-linux-glibc2.12-x86_64.tar.gz下载安装
查看>>
r语言操作mysql_R语言 RMySQL连接操作mysql数据库
查看>>
mysql 整形相除_整型相除截断的技巧
查看>>
mysql备份字符集_浅谈MySQL备份字符集的问题
查看>>
dos下设置mysql密码_dos命令下修改mysql密码的方法
查看>>
交换机如何设置我能访问它但他不能访问我_“交换机”有什么作用?怎样使用?...
查看>>
数据结构基本操作_R中的数据结构简介及类别变量的基本操作
查看>>
微分方程解法总结_视频教学:线性微分方程解的结构、问题类型及求解思路与方法...
查看>>
blt功能_bitblt()用法
查看>>
MySQL中level的用法_leveldb使用 (转载)
查看>>
卷积神经网络由谁提出_科研人员提出一种基于卷积循环神经网络的单通道渐进语音增强方法...
查看>>
python 曲线拟合求参数_Python:查找任意曲线的拟合参数数量
查看>>
python批量生成图片_python日常实用技能:如何利用Python批量生成任意尺寸的图片...
查看>>
python爱好者社区公众号历史文章合集_GitHub - thinkingpy/weixin_crawler: 高效微信公众号历史文章和阅读数据爬虫powered by scrapy...
查看>>
ranger安装hbase插件_ranger的配置与使用
查看>>
mysql模板文件_MySQL 配置文件模板
查看>>