Precedence and binding of C-language operators

Source: Internet
Author: User
Tags logical operators

Today went over the C language operators of the priority and binding, found that the original learning time did not seriously remember, ashamed. Found a well-spoken article, edited the next turn for later browsing.

C language operator precedence table (top-down, descending priority)

Operator

Binding nature

() [].

From left to right

! ~ + +---(type) * & 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

Assignments

From right to left

,

From left to right

1, () [] and so on is a matter of course the highest priority, in fact, they are not what operators.

2, in addition to the above four kinds of operators, next is the monocular operator, that is! ~ + +---(type) * & sizeof. Remember their order but from right to left Ah! In fact, the combination of examples is very easy to understand, such as i++.

3, followed by binocular operator, is the C language operator priority in the most confusing place. In fact, it's not so scary, you can see it carefully. In the binocular operator, the arithmetic operator has the highest precedence, then the shift operator, then the relational operator, and then the logical operator. But here's what you need to say is that the precedence of,< <= > >= in relational operators is higher than = = = =. In addition, in logical operators, the operation is higher than or the priority of the operation, or in between. Similarly, you can get an analogy between && and | | The priority relationship.

4, after the binocular operator, is the three mesh operator, there is nothing to say.

5. Then the assignment operator, you might confuse the assignment operator with the precedence of the three-mesh operator. No matter, I think you must have written such a statement (if not, please remember!) ): max = (a>b)? a:b; , from this statement, it is not difficult to remember why the assignment operator has a lower priority than the trinocular operator!

6, the comma operator is to split the individual sub-statements (feeling that it is a bit inaccurate, but I think you will understand what I mean), the natural priority is the lowest, I think this should not be very easy to confuse.

Sum up, by operator precedence from high to Low: Monocular operator----operator-----------the comma operator, the assignment operator, for the binocular operator, by operator precedence, from high to Low: arithmetic operator---shift operator, relational operators (where = = and! = have lower precedence) logical operators (bitwise XOR with-〉 or-〉 bitwise OR-〉 logic with-〉 logic or)!

The Union of Operators refers to the organizational direction in which operators of the same precedence operate in expressions, that is, when the precedence of operators on both sides of an operand is the same, the order in which the operands and operators are combined, the C language specifies the binding direction (binding) of the various operators. Most operators are in the direction of "from left to right", that is: first left after right, such as A-b+c, B on both sides have-and + two operators have the same priority, according to the first left after the right combination of direction, b first with a minus sign, perform a-C operation, and then perform the operation of the plus. In addition to the binding from left to right, the C language has three types of operators that join the operation in the direction of right-to-left. That is: The monocular operator, the conditional operator, and the assignment operator. The concept of integration is not in other high-level languages, which is one of the characteristics of C language.


++a or a++ and--a or a--, respectively, are called pre-plus or post-plus and pre-minus or post-decrement operations, all of which are monocular operators. It is important to note that the pre-and post-operation can only be used for variables, not constants and expressions, and that the union direction is right-to-left. For example, when i=6, the value of i++ and the value of I are obtained. Since "-" (minus) "+ +" is the same priority, it should be understood as-(i++), and because of the post-add, so the first-i++ value is-6, and then I value 1 is 7, that is, i=7.


Example 1 Main ()
{

int a=3,b=5,c;
C=a*b+++b;
printf ("c=%d", c);

}


To derive the value of C, first make sense of + + +. The combination of the + + operator is right-to-left, if the expression is understood as: c=a*b+ (++B); the C compiler actually handles the expression as: c= (a*b++) +b, because the C compiler always makes as many characters as possible from left to right, such as i+++j equivalent to (i++) +j. The next step is to solve the problem of a*b++ because the + + operator can only be an integer variable and not an expression or a constant, so a*b++ is obviously a * (b++) instead of (a*b) + +, so the entire expression is c= (A * (b++)) +b and the result is c=20.


Example 2 Main ()
{

int i=1,j;
j=i+++i+++i++;
printf ("i=%d,j=%d/n", i,j);

}


Example 3 Main ()
{

int i=1,m;
M= (++i) + (++i) + (++i);

printf ("i=%d,m=%d/n", i,m);

}


The values of J and M are evaluated by the expression, and both expressions consist of a self-increment operator, an addition operator, and an assignment operator. So, how much are they worth? J=1+1+1=3 or j=1+2+3=6? M=2+3+4=9 or m=4+4+4=12? The result of the machine operation is: i=4,j=3,m=10 (VC6.0). Analysis: operator "+ +", "+" and "=" priority is decremented, in the calculation, the first self-increment operation, then the addition operation, and finally the assignment operation. The self-increment operation also obtains different values according to the different definitions of "i++" and "++i". i+++i+++i++ the first value of I (1) is taken out, as the value of I in the expression of the addition operation to get 3, and then realize three times self-added; The calculation of (++i) + (++i) + (++i) is related to the compiler.


Example 4 Set a=6, To evaluate an assignment expression a+=a-=a-a*a the value.
Because "*" (multiplication sign), "-" (minus) priority is higher than "+ =", "-=", and "*" priority is higher than "-", so first a-a*a, that is, 6-6*6=-30, by "+ =", "-=" for the same priority, and from right to left the combination direction, and then a-=-30 (-30) =6+30=36, the last a+=36, that is, a=a+36=36+36=72, so the value of the assignment expression is a=72.


Example 5 Set m=1,n=2,b=3, To evaluate an assignment expression m+=n-=---b the value.
There are four operators "+ =", "-=", "-" (minus), "--", by operator precedence, should be evaluated---b, but "--" and "-" (minus) have the same priority, such as a right-to-left binding direction, it may be-(--b), or--(-B), which is it? As previously mentioned, the predecessor operation can only be used for variables, not for expressions, and (-B) is not a variable, but an expression, so it can only be-(--b), that is-(3-1) =-2; Then calculate n-=-2, i.e. n=n-(-2) =2-(-2) = 4; Finally, the value of the assignment expression is m=5 by calculating m+=4, which is m=m+4=1+4=5.


When you see a complex C-language expression, you should first perform the operation by priority, and then in the same priority, in the combined direction.

Why (i++) + (j + +) + (k++) with (i++) + (i++), the value is unpredictable

http://tieba.baidu.com/p/1972991734

& represents logical AND or bitwise operations with.
&& Express Logic and short-circuit calculation.

Precedence and binding of C-language operators

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.