《C和指针》操作符的优先级

来源:互联网 发布:html与js的关系 编辑:程序博客网 时间:2024/05/16 06:25

《C和指针》一书介绍操作符优先级

5.4.3 操作符的属性

 

每个操作符的所有属性都列在表5.1所示的优先级表中。表中各个列分别代表操作符、它的功能描述、用法示例、它的结果类型、它的结合性以及当它出现时是否会对表达式的求值顺序施加控制。用法示例提示它是否需要操作数为左值。术语lexp表示左值表达式,rexp表示右值表达式。记住,左值意味着一个位置,而右值意味着一个值。所以,在使用右值的地方也可以使用左值,但是在需要左值的地方不能使用右值。

 

说明:结合性L-R自左向右,R-L自右向左。

 

表5.1                                   操作符的优先级

操作符

描述

用法示例

结果类型

结合性

是否控制求值顺序

()

聚组

(表达式)

与表达式同

N/A

()

函数调用

rexp(rexp,…,rexp)

rexp

L-R

[]

下标引用

Rexp[rexp]

lexp

L-R

.

访问结构成员

Lexp.member_name

lexp

L-R

->

访问结构指针成员

Rexp->member_name

lexp

L-R

++

后缀自增

Lexp++

rexp

L-R

--

后缀自减

Lexp--

rexp

L-R

!

逻辑反

!rexp

rexp

R-L

~

按位取反

~rexp

rexp

R-L

+

单目,表示正值

+rexp

rexp

R-L

-

单目,表示负值

-rexp

rexp

R-L

++

前缀自增

++lexp

rexp

R-L

--

前缀自减

--lexp

rexp

R-L

*

间接访问

*rexp

lexp

R-L

&

取地址

&lexp

rexp

R-L

sizeof

取其长度,以字节表示

Sizeof rexp

Sizeof(类型)

rexp

R-L

(类型)

类型转换

(类型)rexp

rexp

R-L

*

乘法

Rexp * rexp

rexp

L-R

/

除法

Rexp / rexp

rexp

L-R

%

整数取余

Rexp % rexp

rexp

L-R

+

加法

Rexp + rexp

Rexp

L-R

-

减法

Rexp - rexp

Rexp

L-R

<< 

左移位

Rexp << rexp

Rexp

L-R

>> 

右移位

Rexp >> rexp

Rexp

L-R

大于

Rexp > rexp

Rexp

L-R

>=

大于等于

Rexp >= rexp

Rexp

L-R

小于

Rexp < rexp

Rexp

L-R

<=

小于等于

Rexp <= rexp

Rexp

L-R

==

等于

Rexp == rexp

rexp

L-R

!=

不等于

Rexp != rexp

Rexp

L-R

&

位与

Rexp & rexp

Rexp

L-R

^

位异或

Rexp ^ rexp

rexp

L-R

|

位或

Rexp | rexp

Rexp

L-R

&&

逻辑与

Rexp && rexp

Rexp

L-R

||

逻辑或

Rexp || rexp

rexp

L-R

?:

条件操作符

Rexp ? rexp : rexp

rexp

N/A

=

赋值

Lexp = rexp

rexp

R-L

+=

以…加

Lexp += rexp

rexp

R-L

-=

以…减

Lexp -= rexp

Rexp

R-L

*=

以…乘

Lexp *= rexp

Rexp

R-L

/=

以…除

Lexp /= rexp

Rexp

R-L

%=

以…取模

Lexp %= rexp

Rexp

R-L

<<=

以…左移

Lexp <<= rexp

Rexp

R-L

>>=

以…右移

Lexp >>= rexp

Rexp

R-L

&=

以…与

Lexp &= rexp

Rexp

R-L

^=

以…异或

Lexp ^= rexp

Rexp

R-L

|=

以…或

Lexp |= rexp

Rexp

R-L

,

逗号

Rexp, rexp

Rexp

L-R

 

我是照书上打的。讲得比较详细,记录下,方便自己同时也方便他人。

原创粉丝点击