# C++运算符优先级

1()
[]
->
.
::
++
--

(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-- ) ...

2!
~
++
--
-
+
*
&
(type)
sizeof

if( !done ) ...
flags = ~flags;
for( i = 0; i < 10; ++i ) ...
for( i = 10; i > 0; --i ) ...
int i = -1;
int i = +1;
data = *ptr;
int i = (int) floatNum;
int size = sizeof(floatNum);

3->*
.*

ptr->*var = 24;
obj.*var = 24;

4*
/
%

int i = 2 * 4;
float f = 10 / 3;
int rem = 4 % 3;

5+
-

int i = 2 + 3;
int i = 5 - 1;

6<<
>>

int flags = 33 << 1;
int flags = 33 >> 1;

7<
<=
>
>=

if( i < 42 ) ...
if( i <= 42 ) ...
if( i > 42 ) ...
if( i >= 42 ) ...

8==
!=

if( i == 42 ) ...
if( i != 42 ) ...

9&按位与操作符flags = flags & 42;从左到右
10^按位异或操作符flags = flags ^ 42;从左到右
11|按位或操作符flags = flags | 42;从左到右
12&&逻辑与操作符if( conditionA && conditionB ) ...从左到右
13||逻辑或操作符if( conditionA || conditionB ) ...从左到右
14? :三元条件操作符int i = (a > b) ? a : b;从右到左
15=
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=

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;

16,逗号操作符for( i = 0, j = 0; i < 10; i++, j++ ) ...从左到右
• 114
点赞
• 411
收藏
觉得还不错? 一键收藏
• 16
评论
05-02 1227
05-15 1119
08-09 3910
03-23 627
08-27 2404
06-06 1831
05-08
01-06 880
03-21 1056
03-26 2554
02-08 1291

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。