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个: ,

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

 

Comments:

Post a Comment:
Comments are closed for this entry.
About

williamxue

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today