C 運算子優先順序列表

2021-06-06 18:53:10 字數 3343 閱讀 2866

precedence

operator

description

example

associativity1()

->.::

++--

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 right2!

~++---

+*&(type)

sizeof

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 right4*

/%multiplication

division

modulus

int i = 2 * 4;

float f = 10 / 3;

int rem = 4 % 3;

left to right5+

-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 right8==

!=comparison equal-to

comparison not-equal-to

if( i == 42 ) ...

if( i != 42 ) ...

left to right9&

bitwise and

flags = flags & 42;

left to right10^

bitwise exclusive or

flags = flags ^ 42;

left to right11|

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 left15=

+=-=

*=/=

%=&=

^=|=

<<=

>>=

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 left16,

sequential evaluation operator

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

left to right

C 運算子優先順序列表

precedence operator description example associativity1 grouping operator array access member access from a pointer member access from an object scopin...

C 運算子優先順序列表

precedence operator description example associativity1 grouping operator array access member access from a pointer member access from an object scopin...

c 運算子優先順序列表

優先順序 運算子名稱或含義 使用形式 結合方向說明1 陣列下標 陣列名 常量表示式 左到右 圓括號 表示式 函式名 形參表 成員選擇 物件 物件.成員名 成員選擇 指標 物件指標 成員名2 負號運算子 表示式 右到左單目運算子 型別 強制型別轉換 資料型別 表示式 自增運算子 變數名 變數名 單目運...