C 運算優先順序

2021-05-17 21:33:19 字數 3341 閱讀 8825

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 運算子優先順序

c 運算子優先順序 優先順序運算子 描述方向1 scope resolution 範圍解析 left to right 2 suffix postfix increment and decrement 字首 字尾遞增和遞減 function call 函式呼叫 array subscripting ...

C語言運算優先順序

圓方括號 箭頭一句號,自增自減非反負 針強位址長度,乘除,加減,再移位,小等大等 等等不等,八位與,七位異,六位或,五與,四或,三疑,二賦,一真逗。其中 號為乙個等級分段。1級 左結合 圓括號 下標運算子 指向結構體成員運算子 結構體成員運算子。2級 右結合 邏輯非運算子 按位取反運算子 字首增量運...

python運算優先順序

運算子優先順序 下面的優先順序高 運算子 描述 lambda lambda表示式 or 布林 或 and 布林 與 not x 布林 非 innot in 成員測試 isis not 同一性測試 比較 按位或 按位異或 按位與 移位 加法與減法 乘法 除法 與取餘 x,x 正負號 x 按位翻轉 指數...