Combination of priority Operators
1: left
2.-> [] () left
3 ++ --~ ! -+ & * () Sizeof new Delete castname_cast <type> right of the single object Operator
4. *-> * left
5 */% left
6 +-left
7 <> left
8 <=> = left
9 =! = Left
10 & left
11 ^ left
12 | left
13 & left
14 | left
15? : Right
16 = * =/= % = + =-= <= >>=& = | = ^ = right
17 throw left
18, left
Priority of C
I. assignment operators
A value assignment statement is used to assign values of a constant, variable, or expression to another variable. The symbol is '= '. Here is not equal to, but a value is equal to '=.
Note: variables on the left of the value assignment statement must be declared elsewhere in the program.
The assigned variables are called left values because they appear on the left of the assignment statement. The generated values are called right values because they appear on the right of the assignment statement. A constant can only be used as the right value.
For example:
Count = 5;
Total1 = total2 = 0;
You can understand the first assignment statement.
The second value assignment statement assigns 0 values to two variables at the same time. This is because the value assignment statement is calculated from the right to the left, that is, from the right end. In this way, it first total2 = 0; then total1 = total2; then we can't do this?
(Total1 = total2) = 0;
This is not acceptable because total1 = total2 is an expression, and the expression is not allowed on the left of the value assignment statement.
Ii. Arithmetic Operators
In C, there are two single-eye and five binary operators.
Symbol function
+ Single lens
-Single Object negative
* Multiplication
/Division
% Modulo
+ Addition
-Subtraction
The following is an example of a value assignment statement. The above arithmetic operator is used in the expression on the right of the value assignment operator:
Area = height * width;
Num = num1 + num2/num3-num4;
The operator also has an operation order problem. Calculate the multiplication, division, and addition and subtraction first. The first operation is performed between a single object and a single object.
The modulo operator (%) is used to calculate the remainder obtained by Division of two integers. For example:
A = 7% 4;
In the end, the result of a is 3, because the remainder of 7% 4 is 3.
So what should I do if I want to ask their vendors?
B = 7/4;
In this way, B is their business. It should be 1.
Maybe someone doesn't understand. 7/4 should be 1.75. How can it be 1? It must be noted that when two integers are separated, the result is still an integer with no decimal part. To get the decimal part, you can write 7.0/4 or 7/4. 0 in this way, that is, change one of them to a non-integer.
So how can we get the integer part of a real number? This requires forced type conversion. For example:
A = (INT) (7.0/4 );
Because the value of 7.0/4 is 1.75, if you add (INT) to the front, the result is forcibly converted to an integer, and 1 is returned. Think about a = (float) (7/4); what is the final result of?
The single-object subtraction operator is equivalent to taking the opposite value. If it is positive, it becomes negative, and if it is negative, it becomes positive.
The single-object addition operator has no meaning. It is purely used with the single-object subtraction operator.
Iii. logical operators
Logical operators return true or false values based on the value of the expression. In fact, there is no so-called true value or false value in C language, but it is considered that non-0 is the true value, and 0 is the false value.
Symbol function
& Logic and
| Logical or
! Non-logical
When an & operation is performed on an expression, if one of the expressions is false, the total expression is false. The formula is true only when all expressions are true. When the expression performs | operation, the total value is true if one of them is true. The formula is false only when all values are false. Non-logical (!) The operation is to convert the corresponding variable data to the corresponding true/false values. If the original value is false, the logic is not true afterwards. If the original value is true, the logic is not false afterwards.
It is also very important that when the value of the last part of a logical expression does not affect the value of the entire expression, the latter part will not be computed. For example:
A = 2, B = 1;
A | B-1;
Because a = 2, is the true value, so no matter whether the B-1 is the true value, the total expression must be the true value, then the following expression will not calculate.
Iv. Relational operators
Relational operators compare two expressions and return a true/false value.
Symbol function
> Greater
<Less
> = Greater than or equal
<= Less than or equal
= Equal
! = Not equal
All of these operators can understand that the main problem is the difference between equal = and value =.
Some people who have just started to learn the C language are always confused about these two operators. They often encounter errors in some simple problems and cannot find them during their own checks. See the following code:
If (amount = 123 )......
Many newcomers understand that if amount is equal to 123, what will happen. In fact, this line of code is to assign a value of amount = 123 first, and then determine whether the expression is true, because the result is 123, which is the true value, then do the following. If you want to run the command when amount is equal to 123, if (amount = 123 )......
5. Auto-increment and auto-subtraction Operators
This is a special type of operator. The auto-increment operator ++ and the auto-increment operator -- the result of the operation on the variable is increased by 1 and decreased by 1. For example:
-- Couter;
Couter --;
++ Amount;
Amount ++;
In these examples, operators have the same influence on themselves in the front and back. They all add or subtract 1, but when they are used as part of other expressions, the two are different. The operator is placed before the variable, so before the operation, the variable first completes the auto-increment or auto-subtraction operation; if the operator is placed behind, then, the auto-incrementing operation is performed after the variable participates in the expression operation. This may not be clear. Let's look at the following example:
Num1 = 4;
Num2 = 8;
A = ++ num1;
B = num2 ++;
A = ++ num1; in general, this is a value assignment, assigning the value of ++ num1 to A, because the auto-incrementing operator is before the variable, so num1 first increases from 1 to 5, and then assigns it to a, and finally a is 5. B = num2 ++; this is to assign the value of num2 ++ to B, because the auto-increment operator is behind the variable, so num2 is assigned to B first, B should be 8, then num2 increases from 1 to 9.
So what should we do if this happens?
C = num1 ++ num2;
Whether it is c = (num1 ++) + num2; or c = num1 + (++ num2); depending on the compiler, different compilers may have different results. Therefore, in future programming, we should try to avoid the above complex situations.
Vi. compound assignment operators
Among the assignment operators, there is also a unique class of composite assignment operators in C/C ++. They are actually abbreviated forms, making changes to variables more concise.
Total = total + 3;
At first glance, there seems to be a problem with this line of code, which is not possible. In fact, it's still the same way. '=' indicates that the value assignment is not equal. It means that the value itself is added with 3, and then assigned to itself. The above code can also be written as follows:
Total + = 3;
The composite assignment operators include the following:
Symbol function
+ = Addition assignment
-= Subtraction value assignment
* = Multiplication
/= Division assignment
% = Modulo assignment
<= Left shift assignment
>>= Right shift value
& = Bit logic and value assignment
| = Bit logic or value assignment
^ = Bit logical difference or value assignment
Among the ten composite assignment operators above, we will describe the next five values for bitwise operations.
After reading the compound assignment operator above, someone will ask, is there a difference between total = total + 3 and total + = 3? The answer is yes. For expression A = a + 1, expression A is calculated twice. For the composite operator A + = 1, expression A is calculated only once. In general, this difference has little impact on the running of the program, but when the expression is used as the return value of the function, the function is called twice (which will be explained later ), in addition, if you use a common value assignment operator, it will increase the overhead of the program and reduce the efficiency.
VII. Conditional Operators
Conditional operators (? :) It is the only three-object operator in C language. It is used to perform true/false checks on the first expression, and then returns one of the two expressions based on the result.
<Expression 1>? <Expression 2>: <expression 3>
In the operation, we first test the first expression. If it is true, the value of expression 2 is returned. If it is false, the value of expression 3 is returned.
For example:
A = (B> 0 )? B:-B;
When B> 0, A = B; when B is not greater than 0, A =-B; this is the conditional expression. In fact, the above meaning is to assign the absolute value of B to.
8. Comma Operator
In C, multiple expressions can be separated by commas (,). Values of expressions separated by commas (,) are settled separately, but the value of the entire expression is the value of the last expression.
Suppose B = 2, c = 7, D = 5,
A1 = (++ B, c --, D + 3 );
A2 = ++ B, c --, D + 3;
For the first line of code, there are three expressions separated by commas, so the final value should be the value of the last expression, that is, D + 3, which is 8, so a = 8. For the second line of code, there are also three expressions. At this time, the three expressions are a2 = ++ B, c --, D + 3, (this is because the value assignment operator has a higher priority than the comma operator). Although the value of the final expression is 8, a2 = 3.
There are other bit logical operators, displacement operators, and so on. We will explain it again when talking about bit operations.
9. Priority and combination
From the example of the comma operator above, we can see that these operators are computed in a certain order, as if we had to calculate multiplication, division, and addition and subtraction first. Priority and associativity are two important features of operators. associativity are also called computational order. They determine whether each part of an expression is involved in calculation and when to calculate it.
The following describes the priority and associativity of operators used in C language:
Combination of priority Operators
(Maximum) () []->. from left to right
! ~ ++ -- +-* & 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 left to right
? : From right to left
= + =-= * =/= % = <<=> = From right to left
(Lowest), from left to right
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/edg_edu/archive/2009/04/03/4046539.aspx