Operators and computational sequence in C ++
1
. Operator and its priority
Priority |
Operator |
Combination Law |
From high to low |
() []->. |
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 Post-increment (subtraction) (lvalue ++) of ++ is greater *. The preincrement (subtraction) (++ lvalue) and * have the same priority.
* P ++ means * (p ++), not (* P) ++.
In this way, the difference is to treat the pre-increment and post-increment: Y = ++ X is equivalent to Y = (x + = 1), while y = x ++ is equivalent to Y = (t, X + = 1, t) The difference looks pretty good.
2
. What is the left value?The left value (lvalue) is an expression that can be assigned values. The left value is on the left of the value assignment statement, and its relative right value (rvaule) is on the right of the value assignment statement. Each value assignment statement must have a left value and a right value.
The left value must be a stored variable in the memory, rather than a constant.
The left value can store the expression results.The result of ++ X is stored in X, so it is the left value. The result of X ++ is not the value in X, so it is not the left value. The right value is not accessible, such as constants, function return values, type conversion results; Int & U (); it returns the left value, (a = 4) = 28; // A = 4 is the left expression.
3
. Value OrderIn an expression, the order in which subexpressions are evaluated is not defined. Specifically, you cannot assume that the expression is evaluated from left to right. Int X = F (3) + g (7) does not define F (), g () which is called first. Int I = 1; V [I] = I ++; the result is also not defined, either V [1] = 1, or V [2] = 1. However, there are three operators with defined computing sequence: comma (,), logic and (&), logic or (| ). They ensure that the operation object on the left must be computed before the operation object on the right. For example, after B = (a = 2, a ++) is calculated, 3 is assigned to B. & Only when the first operation object is true is considered the second operation. | Only when the first operation object is false is considered as the second operation. This is called
Short Circuit Evaluation. Note the following two usage methods: F1 (V [I], I ++) and F2 (V [I], I ++) comma expressions; for F2, only one parameter is equivalent to I ++.