浅谈C/C++中运算符的优先级、运算符的结合性以及操作数的求值顺序

来源:互联网 发布:手机摇号用什么软件 编辑:程序博客网 时间:2024/05/17 22:43

一.运算符的优先级

    在C++ Primer一书中,对于运算符的优先级是这样描述的:

    Precedence specifies how the operands are grouped. It says nothing about the order in which the operands are evaluated.

    意识是说优先级规定操作数的结合方式,但并未说明操作数的计算顺序。举个例子:

    6+3*4+2

    如果直接按照从左到右的计算次序得到的结果是:38,但是在C/C++中它的值为20。

    因为乘法运算符的优先级高于加法的优先级,因此3是和4分组到一起的,并不是6与3进行分组。这就是运算符优先级的含义。

二.运算符的结合性

    Associativity specifies how to group operators at the same precedence level.

    结合性规定了具有相同优先级的运算符如何进行分组。

    举个例子:

    a=b=c=d;

    由于该表达式中有多个赋值运算符,到底是如何进行分组的,此时就要看赋值运算符的结合性了。因为赋值运算符是右结合性,因此该表达式等同于(a=(b=(c=d))),而不是(a=(b=c)=d)这样进行分组的。

    同理如m=a+b+c;

   等同于m=(a+b)+c;而不是m=a+(b+c);

三.操作数的求值顺序

   在C/C++中规定了所有运算符的优先级以及结合性,但是并不是所有的运算符都被规定了操作数的计算次序。在C/C++中只有4个运算符被规定了操作数的计算次序,它们是&&,||,逗号运算符(,),条件运算符(?:)。

   如m=f1()+f2();

   在这里是先调用f1(),还是先调用f2()?不清楚,不同的编译器有不同的调用顺序,甚至相同的编译器不同的版本会有不同的调用顺序。只要最终结果与调用次序无关,这个语句就是正确的。这里要分清楚操作数的求值顺序和运算符的结合性这两个概念,可能有时候会这样去理解,因为加法操作符的结合性是左结合性,因此先调用f1(),再调用f2(),这种理解是不正确的。结合性是确定操作符的对象,并不是操作数的求值顺序。

    同理2+3*4+5;

    它的结合性是(2+(3*4))+5,但是不代表3*4是最先计算的,它的计算次序是未知的,未定义的。

    比如3*4->2+3*4->2+3*4+5

    以及2->3*4->2+3*4->2+3*4+5和5->3*4->2+3*4->2+3*4+5这些次序都是有可能的。虽然它们的计算次序不同,但是对最终结果是没有影响的。

   

C优先级列表

PrecedenceOperatorDescriptionExampleAssociativity1

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

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 right

2

!
~
++
--
-
+
*
&
(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 right

5

+
-

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 right

8

==
!=

Comparison equal-to
Comparison not-equal-to

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

left to right

9

&

Bitwise AND

flags = flags & 42;

left to right

10

^

Bitwise exclusive OR

flags = flags ^ 42;

left to right

11

|

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 left

15

=
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=

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 left

16

,

Sequential evaluation operator

for( i = 0, j = 0; i < 10; i++, j++ ) ...

left to right

 

C++优先级列表

 

PrecedenceOperatorDescriptionExampleAssociativity1

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

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 left3

->*
.*

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 right6

<<
>>

Bitwise shift left
Bitwise shift right

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

left to right7

<
<=
>
>=

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 ANDflags = flags & 42;left to right10^Bitwise exclusive ORflags = flags ^ 42;left to right11|Bitwise inclusive (normal) ORflags = flags | 42;left to right12&&Logical ANDif( conditionA && conditionB ) ...left to right13||Logical ORint i = (a > b) ? a : bleft to right14? :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 operatorfor( i = 0, j = 0; i < 10; i++, j++ ) ...left to right
分类: C/C++

原创粉丝点击