c++提供了一元操作符和二元操作符,另外還有三元操作符(三目操作符cond?expr1:exprl2)。
算術運算子
一元正號(+)返回運算元本身,對運算元不做任何修改。
邏輯運算子
存在短路求值。
優先順序表如下:
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
int i = (a > b) ? a : b
left to right
14? :
ternary conditional (if-then-else)
if( conditiona || conditionb ) ...
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 Primer學習筆記 4 表示式
第四章 表示式 1.在實際情況下,子表示式的計算順序通常是初學者出錯的根源,因此在你記不住某些操作符的計算順序時,加括號明確指定計算順序。2.只要能夠得到表示式的值 true 或false 運算就會結束,給定以下形式 expr1 expr2 expr1 expr2 如果下列條件有乙個滿足 在邏輯與表...
C Primer 5th學習筆記3 表示式
運算子 功能描述 一元正號 expr 一元負號 expr 乘法 expr expr 除法 expr expr 求餘 expr expr 加法 expr expr 減法 expr expr 上述運算子的優先順序中,由上往下,一元運算子優先順序最高,其次是乘除運算,最後是加減運算,所有的運算子都滿足左結...
C Primer 筆記十四 表示式
表示式將運算子作用於乙個或多個運算物件,每個表示式都有對應的求值結果。表示式本身也可以作為運算物件構成對多個運算子求值的復合表示式。運算子 運算子操作物件數量 一元運算子 乙個二元運算子 兩個,不要求型別相同,能轉換成同型別即可 三元運算子 三個函式呼叫 不限過載運算子 overloaded ope...