王朝百科
分享
 
 
 

运算符优先级

王朝百科·作者佚名  2009-12-04  
宽屏版  字体: |||超大  

c语言的操作符共有15个优先级,如下:

Operators Associativity

() [] -> . left to right

! ~ ++ -- + - * (type) sizeof right to left

* / % left to right

+ - left to right

<< >> left to right

< <= > >= left to right

== != left to right

& left to right

^ left to right

| left to right

&& left to right

|| left to right

?: right to left

= += -= *= /= %= &= ^= |= <<= >>= right to left

, left to right

优先级从上到下依次递减,最上面具有最高的优先级,逗号操作符具有最低的优先级。

所有的优先级中,只有三个优先级是从右至左结合的,它们是单目运算符、条件运算符、赋值运算符。其它的都是从左至右结合。

具有最高优先级的其实并不算是真正的运算符,它们算是一类特殊的操作。()是与函数相关,[]与数组相关,而->及.是取结构成员。

其次是单目运算符,所有的单目运算符具有相同的优先级,因此在我认为的 真正的运算符中它们具有最高的优先级,又由于它们都是从右至左结合的,因此*p++与*(p++)等效是毫无疑问的。

接下来是算术运算符,*、/、%的优先级当然比+、-高了。

移位运算符紧随其后。

其次的关系运算符中,< <= > >=要比 == !=高一个级别,不大好理解。

所有的逻辑操作符都具有不同的优先级(单目运算符出外,!和~)

逻辑位操作符的"与"比"或"高,而"异或"则在它们之间。

跟在其后的&&比||高。

接下来的是条件运算符,赋值运算符及逗号运算符。

在C语言中,只有4个运算符规定了运算方向,它们是&&、| |、条件运算符及赋值运算符。

&&、| |都是先计算左边表达式的值,当左边表达式的值能确定整个表达式的值时,就不再计算右边表达式的值。如 a = 0 && b; &&运算符的左边位0,则右边表达式b就不再判断。

在条件运算符中。如a?b:c;先判断a的值,再根据a的值对b或c之中的一个进行求值。

赋值表达式则规定先对右边的表达式求值,因此使 a = b = c = 6;成为可能。

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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
如何用java替换看不见的字符比如零宽空格&#8203;十六进制U+200B
 干货   2023-09-10
网页字号不能单数吗,网页字体大小为什么一般都是偶数
 干货   2023-09-06
java.lang.ArrayIndexOutOfBoundsException: 4096
 干货   2023-09-06
Noto Sans CJK SC字体下载地址
 干货   2023-08-30
window.navigator和navigator的区别是什么?
 干货   2023-08-23
js获取referer、useragent、浏览器语言
 干货   2023-08-23
oscache遇到404时会不会缓存?
 干货   2023-08-23
linux下用rm -rf *删除大量文件太慢怎么解决?
 干货   2023-08-08
刀郎新歌破世界纪录!
 娱乐   2023-08-01
js实现放大缩小页面
 干货   2023-07-31
生成式人工智能服务管理暂行办法
 百态   2023-07-31
英语学习:过去完成时The Past Perfect Tense举例说明
 干货   2023-07-31
Mysql常用sql命令语句整理
 干货   2023-07-30
科学家复活了46000年前的虫子
 探索   2023-07-29
英语学习:过去进行时The Past Continuous Tense举例说明
 干货   2023-07-28
meta name="applicable-device"告知页面适合哪种终端设备:PC端、移动端还是自适应
 干货   2023-07-28
只用css如何实现打字机特效?
 百态   2023-07-15
css怎么实现上下滚动
 干货   2023-06-28
canvas怎么画一个三角形?
 干货   2023-06-28
canvas怎么画一个椭圆形?
 干货   2023-06-28
canvas怎么画一个圆形?
 干货   2023-06-28
canvas怎么画一个正方形?
 干货   2023-06-28
中国河南省郑州市金水区蜘蛛爬虫ip大全
 干货   2023-06-22
javascript简易动态时间代码
 干货   2023-06-20
感谢员工的付出和激励的话怎么说?
 干货   2023-06-18
 
>>返回首页<<
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有