This section focuses on the precedence, binding, and evaluation order of the basic operators.

First look at an expression-(1 + 2) * 3 + (4 + 5 * (6 + 7))

Remember what you did with it, and after reading the following, you might look at the expression in a different way.

Basic operator = +-*/

There is no exponential operation in C .

Operator is the operand, and the operand is something that is placed on either side of the operator.

(1) Assignment operator =

Year = 2016; Read to assign a value of 2016 toyear instead of year equal to 2016.

= Assigns 2016 to the variable year, which is right-to-left, i.e. = has right-associative.

= year; This notation is wrong and cannot be assigned to a constant value.

So, what kind of object should I put on the left side?

The answer is that it must be a storage location . Year is a variable name that represents an address where values can be stored.

Year can be assigned, and we call this class of entities that can be assigned values as modifiable lvalue values .

rvalue refers to the amount that can be assigned to a modifiable lvalue, and of course, the right value does not have to care about modifying it.

What are the operands of the = operator?

(2) Addition operator +

+ Add the values of the sides together.

The operand of the + can be either a variable or a constant.

(3) Subtraction operator –

-Subtract the number behind it from the number in front of it.

-the operand can be either a variable or a constant.

(4) Symbol operator +-

First look at an expression:

-(1990-2016)

The first minus-and the second minus-mean the same thing?

The answer is not the same.

+-called the two-tuple arithmetic operator when acting on two operands, called a unary sign operator when they are acting on an operand.

-When acting on an operand, it is used to indicate or change its algebraic notation.

Unary operator + in the C90 standard is added to C , which is not allowed until +1990 of such statements. Unary operator + does not change the value or symbol of its operand.

(5) Multiplication operator *

* Multiplies the left and right two operands.

* The operand can be a variable or it can be a constant.

(6) Division operator/

Why are the operands always strong when speaking the preceding operators? Because some operators do not work on certain types, some operators have different results for different operands .

/left value is removed by the value on the right.

/For floating-point numbers, the result is a floating-point number, such as 2.0/3.0 , with a result of 0.666667 (why not 0.666666?). ）

/for integers, the result is an integer, such as the result of 2/3 is 0, not 0.6 or 0.7, because since C99 , integer division uses a zero-truncation method, directly truncating the digits after the decimal point.

2.0/3.0, 2/3 The results are all clear, so what is the result of 2.0/3 or 2/3.0 ?

First, there is a sense that if the arithmetic operator has different types of operands on both sides, the type conversion is caused .

In 2.0/3 or 2/3.0, the integer value is first promoted to a floating point type, and then the division operation, the result is 0.666667 (why not 0.666666?). ）

Since it is the end of the zero, **How can we achieve rounding in C? **

It is possible to skillfully use a zero-ending approach, in the form of a positive number a (int) (a+0.5), and a negative number a (int) (a-0.5).

(7) Precedence, precedence, and evaluation order of operators

What is the result of the following expression? What is the order of operations?

-(1 + 2) * 3 + (4 + 5 * (6 + 7))

According to our normal calculation of thinking, will be calculated first 2*3, the first plus before the result is 6, and then calculate the value of the expression in parentheses later.

Before we calculate the above expression, let's look at a simple expression to explain some of the concepts.

2*3+4*5

Think, when you just calculated, when the **priority? **

when two operators share the same operand, the precedence determines the order of evaluation . 2*3+4*5, 4 is + and * shared, the priority is the first to multiply the 4, which ensures that two * operations before the + operation, in 2*3+4*5 this expression, the role of precedence is over.

Then consider,**2*3 and 4*5 first calculate Which? **

You might think that the 2*3 is calculated first, and the reason is the binding of the addition operator +. + as a two-dollar operator, the binding is left to right, so the left side of the + is calculated first, then the right side. If you think like that, you have a wrong understanding of the bond.

**What is the associative nature of operators?**

We look at the expression 2*3+4*5-6*7, after the multiplication operation, the expression is simplified to 6 + 20-42, then we should calculate the 6+20, and then calculate 26-42, because there is the same priority + and-share the same operand 20, so + play a binding role. Remember that the binding is appropriate for operators that share the same operand (note the distinction between the effects of precedence). In 2*3+4*5, two * do not share the same operand, and the multiplication is done first, and after simplification to 6+20, no operand is shared. So the idea on top is wrong.

**Which multiplication is calculated first?**

2*3+4*5, in the end, which multiplication is the advanced line? The answer is not sure, see the concrete realization , by the designer to decide. In particular, it is important to note that the precedence only guarantees multiplication before addition, and does not determine which of the two multiplication takes place first.

Knowing the concept above, let's look at the expression

- (1 + 2) * 3 + (4 + 5 * (6 + 7 ))

Based on the priority, the expression in parentheses is calculated first, which has three parentheses ().

It can be determined that the expressions in red brackets are in green brackets, but the blue brackets and green brackets are the first to be calculated, depending on the implementation.

Notice that the first half (1+2) is -3*3 after the operation, and then a 3 and a unary sign operator before it is combined with-3, and then multiplied by 3.

Tour of C (i) operators--precedence, binding, and Order of evaluation