1. Use of priority Operators
I believe that all people who have played C have had such or such incredible experiences, and they will feel very deeply profound and profound about C, most of the time, you do not know the actual execution of a statement (unless you come to a conclusion after compiling and executing a specific environment, you may still be confused afterwards). In fact, some of the features that have become a reality are not what you want to see when you originally designed the C language, because the priority of some operators is wrong. If you don't believe it, let's take a look at the example below:
I = 1, 2;
What do you think is the result of I? We know that the value of the comma operator is the value of the rightmost operand. However, the assignment priority here is higher, so the actual situation should be:
(I = 1), 2;/* the value of I is 1 */
If the negative I value is 1, the constant 2 is executed, and the calculation result is discarded. The final result of I is 1 rather than 2.
2. Common priority operators that are most prone to mistakes
1) "." has a higher priority "*"
Example: * p. f
Possible mistaken result: field f, (* p). f of the object referred to by p
Actual result: Take the f offset of p as the pointer and then remove the reference * (p. f)
2) "[]" has a higher priority "*"
Example: int * ap []
Possible mistaken result: ap is a pointer to the int array int (* ap) []
Actual result: ap is an array of int x elements (ap []).
3) The priority of the function "()" is higher than that "*"
Example: int * fp ()
Possible mistaken result: fp is a function pointer, which returns int, int (* fp )()
Actual result: fp is a function that returns the int *, int * (fp () type ())
4) "=" and "! = "The priority is higher than the bitwise Operator
Example: (val & mask! = 0)
Possible mistaken result: (val & mask )! = 0
Actual result: val & (mask! = 0)
5) "=" and "! = "The priority is higher than the value assignment.
Example: c = getchar ()! = EOF
Possible mistaken result: (c = getchar ())! = EOF
Actual result: c = (getchar ()! = EOF)
6) arithmetic operators take precedence over Shift Operators
Example: msb <4 + lsb
Possible mistaken result: (msb <4) + lsb
Actual result: msb <(4 + lsb)
7) the comma operator has a lower priority than all other operators.
Example: I = 1, 2
Possible mistaken result: I = (1, 2)
Actual results: (I = 1), 2
For most of these operators, if you sit down and think about them, it will become clear, but like the '7) 'may make many programmers feel at ease. Therefore, if there are Boolean operations, arithmetic operations, bit operations, and other mixed operations in the expression, you 'd better add parentheses where appropriate, but it is clear, it turns out to be a good style.
3. concatenation of Operators
The operator priority is used to determine the degree of closeness between operands in expressions without parentheses. If an operator with the same priority appears, the operator's tuberculosis begins to play a role.
Example: int a, B = 1, c = 2;
A = B = c;
For this instance, if "B = c" is executed first, the result of a is 2. If "a = B" is executed first, the result of a is 1. In fact, all assignments (including those that conform to the assignment) are right-bound. Therefore, the result of a is 2.
Similarly, operators with left associativity are executed from left to right, such as bitwise operators "&" and "| ". Associativity are used for more than two operators with the same priority in an expression. In fact, all operators with the same priority have the same associativity.
4. Summary
In the C language, some of the problems related to sequence are well defined, such as priority and combination, and some are vague. For example, the computing sequence of each operand in most expressions is uncertain, he aims to allow compiler designers to select the most appropriate method to generate the fastest code. However, it should be noted that in function calls, the calculation sequence of each parameter is uncertain.