运算符优先级

运算符优先级

 
在一个表达式中可能包含多个有不同运算符连接起来的、具有不同数据类型的数据对象;由于表达式有多种运算,不同的运算顺序可能得出不同结果甚至出现错误运算错误,因为当表达式中含多种运算时,必须按一定顺序进行结合,才能保证运算的合理性和结果的正确性、唯一性。
优先级从上到下依次递减,最上面具有最高的优先级,逗号操作符具有最低的优先级。表达式的结合次序取决于表达式中各种运算符的优先级。优先级高的运算符先结合,优先级低的运算符后结合,同一行中的运算符的优先级相同。[1]  
中文名
运算符优先级
结合次序
表达式中各种运算符的优先级
最高优先级
是一类特殊的操作
单目运算符
具有相同的优先级
 

目录

  1. 优先级
  2. C语言
  3. C++
  4. C#

优先级编辑

优先级从上到下依次递减,最上面具有最高的优先级,逗号操作符具有最低的优先级。
相同优先级中,按结合顺序计算。大多数运算是从左至右计算,只有三个优先级是从右至左结合的,它们是 单目运算符条件运算符赋值运算符
基本的优先级需要记住:
指针最优,单目运算优于双目运算。如正负号。
先乘除(模),后加减。
先算术运算,后移位运算,最后位运算。请特别注意:1 << 3 + 2 & 7等价于 (1 << (3 + 2))&7.
逻辑运算最后计算。

C语言编辑

优先级
运算符
名称或含义
使用形式
结合方向
说明
1
[ ]
数组下标
数组名[整型表达式]
左到右
 
( )
圆括号
(表达式)/函数名(形参表)
 
.
成员选择(对象)
对象.成员名
 
->
成员选择(指针)
对象指针->成员名
 
2
-
负号运算符
-表达式
右到左
单目运算符
(类型)
强制类型转换
(数据类型)表达式
 
++
自增运算符
++变量名/变量名++
单目运算符
--
自减运算符
--变量名/变量名--
单目运算符
*
取值运算符
*指针表达式
单目运算符
&
取地址运算符
&左值表达式
单目运算符
!
逻辑非运算符
!表达式
单目运算符
~
按位取反运算符
~表达式
单目运算符
sizeof
长度运算符
sizeof 表达式/sizeof(类型)
 
3
/
表达式/表达式
左到右
双目运算符
*
表达式*表达式
双目运算符
%
余数(取模)
整型表达式%整型表达式
双目运算符
4
+
表达式+表达式
左到右
双目运算符
-
表达式-表达式
双目运算符
5
<<
左移
表达式<<表达式
左到右
双目运算符
>>
右移
表达式>>表达式
双目运算符
6
>
大于
表达式>表达式
左到右
双目运算符
>=
大于等于
表达式>=表达式
双目运算符
<
小于
表达式<表达式
双目运算符
<=
小于等于
表达式<=表达式
双目运算符
7
==
等于
表达式==表达式
左到右
双目运算符
!=
不等于
表达式!= 表达式
双目运算符
8
&
按位与
整型表达式&整型表达式
左到右
双目运算符
9
^
按位异或
整型表达式^整型表达式
左到右
双目运算符
10
|
按位或
整型表达式|整型表达式
左到右
双目运算符
11
&&
逻辑与
表达式&&表达式
左到右
双目运算符
12
||
逻辑或
表达式||表达式
左到右
双目运算符
13
?:
条件运算符
表达式1? 表达式2: 表达式3
右到左
三目运算符
14
=
赋值运算符
变量=表达式
右到左
 
/=
除后赋值
变量/=表达式
 
*=
乘后赋值
变量*=表达式
 
%=
取模后赋值
变量%=表达式
 
+=
加后赋值
变量+=表达式
 
-=
减后赋值
变量-=表达式
 
<<=
左移后赋值
变量<<=表达式
 
>>=
右移后赋值
变量>>=表达式
 
&=
按位与后赋值
变量&=表达式
 
^=
按位异或后赋值
变量^=表达式
 
|=
按位或后赋值
变量|=表达式
 
15
,
逗号运算符
表达式,表达式,…
左到右
从左向右顺序运算
以下部分错误:
 
结合性
( ) [ ] -> . ++(后缀自增) --(后缀自减)
left to right
! ~ ++(前缀自增) --(前缀自减) + - * sizeof(type)
right to left
* / %
 

  

  

  
left
to
right
+ -
<< >>
< <= > >=
== !=
&
^
|
&&
||
? : (条件运算)
right to left
= += -= *= /= %= &= ^= |= <<= >>=
 
left to right
接下来是算术运算符,*、/、%的优先级当然比+、-高了。
移位运算符紧随其后。
其次的关系运算符中,< <= > >=要比 == !=高一个级别,不大好理解。
所有的逻辑操作符都具有不同的优先级( 单目运算符除外,!和~)
逻辑位操作符的"与"比"或"高,而"异或"则在它们之间。
跟在其后的&&比||高。
接下来的是条件运算符, 赋值运算符及逗号运算符。
在C语言中,只有4个运算符规定了运算方向,它们是&&、| |、条件运算符及逗号运算符。
&&、| |都是先计算左边 表达式的值,当左边表达式的值能确定整个表达式的值时,就不再计算右边表达式的值。如 a = 0 && b; &&运算符的左边位为0,则右边表达式b就不再判断。
在条件运算符中。如a?b:c;先判断a的值,再根据a的值对b或c之中的一个进行求值。
逗号表达式则规定从左到右依次进行求值。

C++编辑

Operator
Description
Example
Overloadable
Group 1(no associativity)
   
::
Scope resolution operator
Class::age = 2;
NO
Group 2
   
()
Function call
isdigit('1')
YES
()
Member initalization
c_tor(int x, int y) : _x(x), _y(y*10){};
YES
[]
Array access
array[4] = 2;
YES
->
Member access from a pointer
ptr->age = 34;
YES
.
Member access from an object
obj.age = 34;
NO
++
Post-increment
for( int i = 0; i < 10; i++ ) cout << i;
YES
--
Post-decrement
for( int i = 10; i > 0; i-- ) cout << i;
YES
const_cast
Special cast
const_cast<type_to>(type_from);
NO
dynamic_cast
Special cast
dynamic_cast<type_to>(type_from);
NO
static_cast
Special cast
static_cast<type_to>(type_from);
NO
reinterpret_cast
Special cast
reinterpret_cast<type_to>(type_from);
NO
typeid
Runtime type information
cout &laquo; typeid(var).name();
cout &laquo; typeid(type).name();
NO
Group 3(right-to-left associativity)
   
!
Logical negation
if( !done ) …
YES
not
Alternate spelling for !
  
~
Bitwise complement
flags = ~flags;
YES
compl
Alternate spelling for ~
  
++
Pre-increment
for( i = 0; i < 10; ++i ) cout << i;
YES
--
Pre-decrement
for( i = 10; i > 0; --i ) cout << i;
YES
-
Unary minus
int i = -1;
YES
+
Unary plus
int i = +1;
YES
*
Dereference
int data = *intPtr;
YES
&
Address of
int *intPtr = &data;
YES
new
Dynamic memory allocation
long *pVar = new long;
MyClass *ptr = new MyClass(args);
YES
new []
Dynamic memory allocation of array
long *array = new long[n];
YES
delete
Deallocating the memory
delete pVar;
YES
delete []
Deallocating the memory of array
delete [] array;
YES
(type)
Cast to a given type
int i = (int) floatNum;
YES
sizeof
Return size of an object or type
int size = sizeof floatNum;
int size = sizeof(float);
NO
Group 4
   
->*
Member pointer selector
ptr->*var = 24;
YES
.*
Member object selector
obj.*var = 24;
NO
Group 5
   
*
Multiplication
int i = 2 * 4;
YES
/
Division
float f = 10.0 / 3.0;
YES
%
Modulus
int rem = 4 % 3;
YES
Group 6
   
+
Addition
int i = 2 + 3;
YES
-
Subtraction
int i = 5 - 1;
YES
Group 7
   
<<
Bitwise shift left
int flags = 33 << 1;
YES
>>
Bitwise shift right
int flags = 33 >> 1;
YES
Group 8
   
<
Comparison less-than
if( i < 42 ) …
YES
<=
Comparison less-than-or-equal-to
if( i <= 42 ) ...
YES
>
Comparison greater-than
if( i > 42 ) …
YES
>=
Comparison greater-than-or-equal-to
if( i >= 42 ) ...
YES
Group 9
   
==
Comparison equal-to
if( i == 42 ) ...
YES
eq
Alternate spelling for ==
  
!=
Comparison not-equal-to
if( i != 42 ) …
YES
not_eq
Alternate spelling for !=
  
Group 10
   
&
Bitwise AND
flags = flags & 42;
YES
bitand
Alternate spelling for &
  
Group 11
   
^
Bitwise exclusive OR (XOR)
flags = flags ^ 42;
YES
xor
Alternate spelling for ^
  
Group 12
   
|
Bitwise inclusive (normal) OR
flags = flags | 42;
YES
bitor
Alternate spelling for |
  
Group 13
   
&&
Logical AND
if( conditionA && conditionB ) …
YES
and
Alternate spelling for &&
  
Group 14
   
||
Logical OR
if( conditionA || conditionB ) ...
YES
or
Alternate spelling for ||
  
Group 15(right-to-left associativity)
   
? :
Ternary conditional (if-then-else)
int i = (a > b) ? a : b;
NO
Group 16(right-to-left associativity)
   
=
Assignment operator
int a = b;
YES
+=
Increment and assign
a += 3;
YES
-=
Decrement and assign
b -= 4;
YES
*=
Multiply and assign
a *= 5;
YES
/=
Divide and assign
a /= 2;
YES
%=
Modulo and assign
a %= 3;
YES
&=
Bitwise AND and assign
flags &= new_flags;
YES
and_eq
Alternate spelling for &=
  
^=
Bitwise exclusive or (XOR) and assign
flags ^= new_flags;
YES
xor_eq
Alternate spelling for ^=
  
|=
Bitwise normal OR and assign
flags |= new_flags;
YES
or_eq
Alternate spelling for |=
  
<<=
Bitwise shift left and assign
flags <<= 2;
YES
>>=
Bitwise shift right and assign
flags >>= 2;
YES
Group 17
   
throw
throw exception
throw EClass(“Message”);
NO
Group 18
   
,
Sequential evaluation operator
for( i = 0, j = 0; i < 10; i++, j++ ) …
YES

C#编辑

优先级
类别
运算符
1
基本
(x) x.y f(x) a[x] x++ x――new typeof sizeof checked unchecked
2
单目
+ - ! ~ ++x ――x (T)x
3
乘法与除法
* / %
4
加法与减法
+ -
5
移位运算
<< >>
6
关系运算
< > < = >=
7
条件等
= = ! =
8
位逻辑与
&
9
位逻辑异或
^
10
位逻辑或
|
11
条件与
&&
12
条件或
13
条件
?:
14
赋值
= *= /= %= += -= <<= >>= &= ^= |=
参考资料
  • 1.  张桂珠、张平、陈爱国.Java面向对象程序设计(jdk1.6)第三版:北京邮电大学出版社,2005

转载于:https://www.cnblogs.com/Impulse/p/4903541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值