Operators and calculation order in C ++

xiaoxiao2021-03-18  217

C

Operator and calculation order

1. Operator and its priority

priority

Operator

Binding law

From

high

Until

low

row

Column

() [] ->.

from left to right

! ~ - (Type)

SIZEOF - * &

from right to left

* /%

from left to right

-

from left to right

<< >>

from left to right

<

<=>> =

from left to right

==! =

from left to right

&

from left to right

^

from left to right

|

from left to right

&&&&

from left to right

||

from right to left

?:

from right to left

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

from left to right

In the C programming language, the priority of the amount (LValue ) is greater than *. Pre-increasing (decrease) amount ( Lvalue) and * have the same priority.

* P means * (p ), not (* p) .

This differences are former increments and posts: y = x equivalent to y = (x = 1), and y = x is equivalent to Y = (t, x = 1, t) differences seems quite Nice.

2

. What is the left value?

Left value

(LVALUE)

It is an expression that can be assigned. The left value is located on the left side of the assignment statement, with its opposite right value (

RVAule

)

,

Then located on the right side of the assignment statement. Each assignment statement must have a left value and a right value. The left value must be a stored variable in memory, not a constant.

The left value can store the result of the expression: X's result is stored in X, so it is the left value. The result of x is not a value placed in X, so it is not a left value.

The right value is unsitable, such as constants, functions return values, type conversion results;

INT & u (); it returns the left value, (a = 4) = 28; // a = 4 is a left value expression.

3

. Quotation order

In an expression, the order of the sub-expression is not defined. In particular, you can't assume the expression from left to right. INT x = f (3) g (7) does not define f (), g () which one is called.

INT i = 1; v [i] = i ;

The result is also not defined or V [1] = 1, or V [2] = 1.

However, there are three computational order of three operators that are defined: comma (,), logic and (&&), logic or (| |). They guarantee that the arithmetic objects on the left must calculate the calculation object on the right. 3 assign 3 to b after: b = (a = 2, A ). && only counts the second when the first operation object is TRUE. | | Only when the first calculation object is false, this is called

Short circuit evaluation.

Pay attention to two uses of comma:

F1 (v [i], i )

Two parameters;

F2 ((v [i], i ))

Comma expression;

For F2, only one parameter is equivalent to i .

转载请注明原文地址:https://www.9cbs.com/read-129772.html

New Post(0)