Partitioning and understanding of the precedence of 38 common operators in C # _c# tutorial

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators
In C #, there are 38 commonly used operators, based on the characteristics of the operations they perform and their precedence, and for the sake of memory, I classify them as seven levels: 1, cell operators, and parentheses. 2. General arithmetic operators. 3, displacement operator. 4, comparison operator. 5, logical operators. 6, various assignment operators. 7, right bit (suffix) cell operator.

1, in this level, there are + + 、--(as a prefix), (), +,-(as a cell operator) 、!、 ~. This level is a cell operator except for the pair of special parentheses that have the precedence of any operation. This shows that in the definition expression, those cell operators have a high precedence, probably because they all work directly on the operands. Only the precedence of two cell operators is not in the first level, and they appear in the back for special reasons.

2, in the conventional arithmetic operators, we often use the *,/,%, +,-because they use more, so also ranked in a higher position.

3, this level is two special bit operators,<< and &GT;&GT, they are all the two-element operators in addition to the general operator precedence of the highest, may be because compared to comparison operators, logical operators, this set of operators or numerical calculation.

4, comparison operators, including <, >, <=, >=, = =,!=, a total of six, there is a feature, is less than precedence over greater than.

5, logical operators, there are four logical operators, but for the unit's "non" operator is in front, so there is only &, ^, |, plus two additional to improve the efficiency of the code of the operators &&, | |, a total of five.

6, the assignment operator, this level most, almost before the two-dollar operator, where the addition of a "=" number becomes an assignment operator. First of all, of course the most basic assignment operator "="; then there are the "*=,/=,%=, + + =" Evolution of the general arithmetic operators, which are in the same order as the regular arithmetic characters, and then the displacement and logical operators, also in the order in which they evolved, for "<<=, > >=, &=, ^=, |= ". Because the comparison operator produces a data type that differs from the data type of its operands, they do not have a corresponding assignment operator.

7, the last level, is the two final cell operator suffix version of the + + 、--。 They appear to be a complement to the two prefix versions of = = 、--(those two are too high, people need less than two priorities) to facilitate design expressions, so these two priorities are of course at the end of the line.

The order of precedence of these 38 operators can then be written as follows: (whether horizontally or vertically, the higher the precedence of the top)
The first level: + + 、--(as prefix), (), +,-(when done as a cell operator) 、!、 ~.
Second level: *,/,%, + 、-。
Third level:<<, >>.
Level fourth:<, >, <=, >=, = =,!=.
Level fifth:&, ^, |, &&, | |.
Level sixth: =, *=,/=,%=, + + =, <<=, >>=, &=, ^=, |=.
Level seventh: + + 、--(as suffix).

This is my own learning in C # Some of the summary, which for all levels of division, the operator's priority is not correct understanding, but their own views, to share with you.

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.