The difference between the precedence and the binding of operators in C language

Source: Internet
Author: User

introduced:The C and Pointers P83 page has an example: A*b+c*d+e*f, and for this example, the precedence and binding of the operator are explained. To facilitate the following instructions, we have numbered the operators in the above expressions: multiplication sign 1, 2, 3 and plus 1, 2 for operator precedence I can also understand that the adjacent operators (for example, multiplication sign 1 and plus 1 are adjacent, plus 1 and multiplication sign 2 are adjacent ...). ) is evaluated in order of precedence and is executed first. This so-called "neighbor" is for a variable (such as B or C in the above expression).
Questions:But for the associativity of the operators, I'm not quite able to understand them. The book explains the "binding": "The two adjacent operators which are first executed depend on their priority, and if they have the same precedence, then their order of execution is determined by their binding". The same operator that is said to be adjacent and has the same precedence, in the above expression, probably refers to two "plus". But these two "plus" at first glance is not adjacent ah, then how to define the adjacency.
My understanding:We should look at this problem of adjacency in a dynamic way. Taking the above expression as an example, multiplication sign 1 and multiplication sign 2 will execute first, and then generate a new value (assumed to be G and h), then the above expression becomes: g+h+e*f; At this point, look at the plus side of H, that is, adjacent operators .... A broader explanation is that "adjacency" in the "neighboring operator" can be thought of as an expression, for example, for the expression c*d, plus 1 and plus 2 are adjacent. Then the explanation is inaccurate.
a further understanding:In fact, "priority" and "bonding" are two different levels of concept."Precedence" determines the order in which operators are executed, the order in which they are defined, and "associativity" determines the direction of operator execution, and defines the order of space. Take G+h+e*f as an example: from the priority point of view, the plus 2 priority level is lower than multiplication sign; from a binding standpoint, the plus sign 1 and the plus 2 should be executed from left to right. Thus, the final requirement is the plus 2 last execution, as for the first plus 1 operation or multiplication, there is no necessary provisions.

The difference between the precedence and the binding of operators in C language

Related Article

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.