C language knowledge (3) — Syntax and Operator precedence (C语言 基本知识3)

C language knowledge (3) — Syntax and Operator precedence (C语言 基本知识3)

 

C
(programming language)

From
Wikipedia, the free encyclopedia

Syntax

Main article: C syntax

Operator precedence

Main article: Operators in C and C++

What follows is the list of C operators sorted from highest to
lowest priority. Operators of same priority are presented on the same
line. "R→L" associativity means that adjacent operators of the same
priority are executed from right to left, and conversely for "L→R".

Class Associativity Operators
Select L→R (…) […] -> .
Unary R→L ! ~ + – * & (type) sizeof ++ —
Binary arithmetical L→R * / %
Binary arithmetical L→R + –
Shift L→R << >>
Comparison L→R < <= > >=
Comparison L→R == !=
Binary bitwise L→R &
Binary bitwise L→R ^
Binary bitwise L→R |
Binary boolean L→R &&
Binary boolean L→R ||
Ternary R→L ?…:
Assignments R→L = += -= *= /= &= |= ^= <<= >>=
Sequence L→R ,

另外有人总结出一个优先级的口诀可以帮助记忆:

醋坛酸味灌
味落跳福豆

共44个运算符

醋-初等,4个: ( ) [ ] -> 指向结构体成员 . 结构体成员
坛-单目,9个: ! ~ ++ — -负号 (类型) *指针 &取地址 sizeof长度
酸-算术,5个: * / % + -减
味-位移,2个: << >>
灌-关系,6个: < <= > >= == 等于 != 不等于
味-位逻,3个: & 按位与 ^ 按位异或 | 按位或
落-逻辑,2个: && 逻辑与 || 逻辑或
跳-条件,1个: 三目: ? :
福-赋值,11个: = += -= *= /= %= >>= <<= &= ^= |=
豆-逗号,1个: ,

结合方向自右向左的只有三类:赋值、单目和三目
同一优先级的运算顺序由结合方向决定

 

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s