C-language combination direction and priority

Source: Internet
Author: User
Tags arithmetic operators bitwise

Re-over the C language, I think the C-language operator binding is necessary to mention, after all, last year I myself just learned C language, I did not understand the problem of integration.

What is left-bound? What is the right combination? What do they do with it? When did they use it?

This article will focus on these issues:

(Little Bai Wen, great god detour)

First, the Appendix a few of their own learning process of reference site. (Some insights and examples come from the website)

Http://wenku.baidu.com/link?url=W2ofiBx4yEtrxAAfV8lUaR-bYSIuBox1_ Dh-1mg6g6q0venbr31ofklauxrq6arqmjeo-r1hvvybjxvvvtfasi5cxi4merolvxzs3yzrtis

Http://c.chinaitlab.com/basic/873233.html

http://bbs.csdn.net/topics/370153775

Before you talk about bonding, you should mention the operators of C and their precedence relationships.

C-language operator precedence

Priority level

Operator

Name or meaning

Use form

Combination direction

Description

1

[]

Array subscript

array name [constant expression]

Left to right

--

()

Parentheses

(expression)/function name (formal parameter list)

--

.

Member selection (object)

Object. Member name

--

-

Member selection (pointer)

Object pointers, member names

--

2

-

Minus sign operator

-expression

Right to Left

Monocular operator

~

Bitwise inverse operator

~ Expression

++

Self-increment operator

+ + variable name/variable name + +

--

Self-decrement operator

--Variable name/variable name--

*

Value operator

* Pointer variable

&

Fetch address operator

& Variable Name

!

Logical non-operator

! An expression

Type

Forcing type conversions

(data type) expression

--

sizeof

Length operator

sizeof (expression)

--

3

/

Except

An expression/expression

Left to right

Binocular operator

*

By

Expression-expression *

%

Remainder (modulo)

Integer expression% integer expression

4

+

Add

An expression + an expression

Left to right

Binocular operator

-

Reducing

Expression-expression

5

<<

Move left

Variables << expressions

Left to right

Binocular operator

>>

Move right

Variables >> expressions

6

>

Greater than

Expressions > expressions

Left to right

Binocular operator

>=

Greater than or equal

Expression->= expression

<

Less than

Expressions < expressions

<=

Less than or equal

Expression-<= expression

7

==

Equals

Expression-= = Expression

Left to right

Binocular operator

! =

Not equal to

Expression! = Expression

8

&

Bitwise-AND

Expressions & Expressions

Left to right

Binocular operator

9

^

Bitwise XOR OR

An expression ^ expression

Left to right

Binocular operator

10

|

Bitwise OR

Expression-expression

Left to right

Binocular operator

11

&&

Logic and

Expressions && Expressions

Left to right

Binocular operator

12

||

Logical OR

An expression | | An expression

Left to right

Binocular operator

13

?:

Conditional operators

Expression 1?

Expression 2: Expression 3

Right to Left

Trinocular operator

14

=

Assignment operators

variable = expression

Right to Left

--

/=

Assign value after addition

Variable-/= expression

--

*=

Multiply post-Assign value

Variable-*= expression

--

%=

Assign value after modulo

Variable-%= expression

--

+=

Add after Assignment

Variable + = expression

--

-=

Reduced-value Assignment

Variable-= expression

--

<<=

Assign value after left shift

Variable-<<= expression

--

>>=

Assign value after right shift

Variable->>= expression

--

&=

Bitwise AND post-assigned values

Variable-&= expression

--

^=

Bitwise XOR or post-assignment

Variable-^= expression

--

|=

Bitwise OR post-assigned value

Variable-|= expression

--

15

Comma operator

expression, expression,...

Left to right

--

Description

operators are divided into 15 levels, 1-level priority, and 15-level precedence.

Operators of the same precedence, the order of operations is determined by the binding direction. (Binding: 2 13 14 is left to right from right to left)
Simple Note is:! > Arithmetic operators > Relational operators > && > | | > Assignment Operators

In a little more detail, it comes with a formula. (Online Search)

bracket member first; The parentheses operator [] () member operator. -   The whole monocular second; All monocular operators such as + +,--、 + (positive),-(negative), pointer operations *, & multiplication Yozo, plus minus four; This "remainder" refers to the residual operation that is%   shift five, relationship six; Shift operators:<< >>, Relationships:> < >= <=, etc.   equal to (and) unequal row seventh; that is = = and! =   bits and XOR and bits or;     "Three points of the world" eighty or ninety; These are bitwise operations: Bits and (&) XOR (^) bits or (|)   logic or heel; Logical operators: | | and &&   12 and 11; Note Order: Priority (| |) Bottom-priority (&&)   conditions above assignment,//three mesh operator precedence to 13-bit only than assignment operator and "," High   lowest comma operation level! The comma operator has the lowest precedence

If you want to learn more about operator precedence, I think the tables and formulas above are sufficient.

With the above foundation, we return to the topic of this article------binding.

Observing the above operator table it is not difficult to find that in the same priority list, there are more than one operator, and when the two operators come together, how to calculate?

Or, two of the same operators together, how to do the operation?

To solve these problems, we must understand the integration.

A notable feature of the C language compared to other high-level languages is that its operators are particularly rich, with a total of 34 operators. The C language prescribes these 34 operators with different precedence and binding. Precedence is used to identify the order in which operators operate in an expression, when the value of an expression is solved, always by the precedence of the operator, from high to low, but when the operator precedence is the same on both sides of an operand, the order of operation of the expression is determined by the binding of the operator.
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 bonding is not in other high-level languages, which is one of the features of the C language, especially right-to-left binding error-prone, following a few specific operators to analyze the binding of C-language operators.
If a is a variable, then ++a or a++ and-A or a--are called pre-plus or post-plus and pre-minus or post-decrement operations, and ++a or a++ is equivalent to a=a+1,-a or a--equivalent to A=a-1, which is to increase the value of the variable by 1 or 1. It can be concluded that the operation structure of a pre-or post-operation is the same for a variable, but when they are combined with other operations in an expression, the operation value is different.

The predecessor operation is the value of the variable plus 1 or minus 1, then the changed value of the variable to participate in other operations, such as x=5; y=8; C=++x*y; After the operation, the value of C is the value of 48,x is the value of 6,y is 8.

The latter operation is the value of the variable first involved in the operation, and then the value of the variable itself is added 1 minus 1, that is, participating in the operation is the value of the variable before the change. such as x=5; y=8; C=x++*y; After the operation, the value of C is the value of 40,x is 6, and the value of Y is 8.

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 increment 1 is 7, that is, i=7.

When it comes to this, everyone should understand. To be blunt, many operators have the same precedence. At this point, the binding of the operators began to play a role. In an expression where there are several operators with the same precedence, the binding acts as a quorum, which determines which operation Mr. Foo executes. Executed first, can be seen as parentheses.  For example, right-bound (right-to-left), that is, the operation of the right side of the parentheses around the first, and then the left side of the calculation, so that the natural first to execute the right.         such as int a,b=1,c=2;  A=b=c; The binding of ' = ' here is from right to left. therefore a=b=c;  Can be changed to a= (B=C); That is a=2.

That's pretty much the story.

In general, mastering the above priorities, the binding is useful, but it is not necessary. After all, we have () This big trick is not, where not sure, where to fill () just.

Study on the road, with June mutual encouragement.

C-language combination direction and priority

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.