The priority of the C Operator is incorrect.

Source: Internet
Author: User

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.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.