C Operator priority

Source: Internet
Author: User
Tags arithmetic operators bitwise operators

Guidance:
Types of operators C-language operators can be divided into the following types:
1. Arithmetic Operators
It is used for various numerical operations. Including addition (+), subtraction (-), multiplication (*), Division (/), remainder (or modulo operation, %), auto-increment (++) and auto-subtraction.
2. Relational operators
Used for comparison. Including greater than (>), less than (=), less than or equal to (3. logical operators
Used for logical operations. Including and (&), or (|), non (!) Three.
4. bitwise operators
The amount involved in the operation, which is calculated in binary bits. Including bitwise AND (&), bitwise OR (|), bitwise non (~) , Bitwise exclusive or (^), left shift (>.
5. Value assignment operator
Used for value assignment, including simple value assignment (=), compound arithmetic value assignment (+ =,-=, * =,/=, % =), and compound bit operation value assignment (& =, | =, ^ =, >>=, 6. conditional Operators
This is a three-object Operator Used for conditional evaluation (? :).
7. Comma Operator
It is used to combine several expressions into one (,).
8. pointer Operators
This operation is used to obtain the content (*) and the address.
9. Number of cell Operators
Used to calculate the number of bytes of the Data Type (sizeof ).
10. Special operators
There are several types of brackets (), subscripts [], and Members (→.
Priority and combination
In C language, the operator priority is divided into 15 levels. Level 1 is the highest, and level 15 is the lowest. In an expression, a higher priority is calculated prior to a lower priority. When the operator priorities on both sides of a computing workload are the same, the operators are processed in the combined direction specified by the operator's conformances. In C language, operators can be combined into two types: left-to-right (from left to right) and right-to-left (from right to left ). For example, the combination of arithmetic operators is from left to right, that is, first left and then right. If there is an expression x-y + Z, y should first be combined with the "-" number to execute the x-y operation, and then execute the + z operation. This combination direction from left to right is called "left combination ". The combination direction from right to left is called "right combination ". The most typical right concatenation operator is the value assignment operator. For example, x = y = Z. Due to the right combination of "=", you should first execute y = Z and then perform the X = (y = z) operation. Many operators in the C language are right-bound. Pay attention to the differences to avoid understanding errors.
Basic arithmetic operators of Arithmetic Operators and arithmetic expressions
1. The addition operator "+" is a binary operator, that is, two quantities are involved in addition. Such as a + B and 4 + 8. Has the right combination.
2. subtraction operator "-" subtraction operator is a binary operator. However, "-" can also be used as a negative value operator. In this case, it is a single-object operation, such as-X and-5, which has a left combination.
3. The multiplication operator "*" is left-bound.
4. The Division operator "/" is left-bound. When the calculation involved is an integer, the result is also an integer, with the decimal number removed. If one of the computations is real, the result is double-precision.
Void main (){
Printf ("/n % d, % d/N", 20/7,-20/7 );
Printf ("% F, % F/N", 20.0/7,-20.0/7 );
}
Binocular computation is left-bound. When the calculation involved is an integer, the result is also an integer, with the decimal number removed. If one of the computations is real, the result is double-precision. Printf ("/n % d, % d/N", 20/7,-20/7 );
Printf ("% F, % F/N", 20.0/7,-20.0/7 );
In this example, the result of 20/7 and-20/7 is an integer and all decimals are removed. And 20.0/7 and-20.0/7 participate in the calculation because of the real number, so the result is also real.
5. Perform binary operations with the remainder operator (modulo operator) "%", which is left-bound. The amount involved in the operation must be an integer. The result of the remainder operation is equal to the remainder after the division of two numbers.
Void main (){
Printf ("% d/N", 100% 3 );
}
Binocular computation, with left associativity. The remainder operator % requires that the amount involved in the operation be an integer. In this example, Divide 100 by 3 and the remainder is 1.
Auto increment 1, auto increment 1 operator
The auto-increment 1 operator is recorded as "++". Its function is to increase the value of a variable by 1. The "--" operator is used to reduce the value of a variable by 1. Auto-increment 1 and auto-increment 1 operators are single-object operations, all of which have the right combination. You can perform the following operations. -- I minus 1 and then participates in other operations.
After I ++ I is involved in the operation, the value of I increases by 1.
After I -- I is involved in the operation, the value of I is reduced by 1.
I ++ and I -- are prone to errors in understanding and using --. Especially when they are in more complex expressions or statements, it is often difficult to find out, so we should analyze them carefully.
Void main (){
Int I = 8;
Printf ("% d/N", ++ I );
Printf ("% d/N", -- I );
Printf ("% d/N", I ++ );
Printf ("% d/N", I --);
Printf ("% d/N",-I ++ );
Printf ("% d/N",-I --);
} I printf ("% d/N", ++ I );
Printf ("% d/N", -- I );
Printf ("% d/N", I ++ );
Printf ("% d/N", I --);
Printf ("% d/N",-I ++ );
Printf ("% d/N",-I --);
The initial value of I is 8.
The output is 9 after adding 1 to the first line I;
3rd rows minus 1, so the output is 8;
4th rows of output I is 8 and then add 1 (9 );
5th the row output I is 9 and then minus 1 (8 );
6th rows output-8 and then add 1 (9 );
7th rows output-9 and then minus 1 (8)
Void main (){
Int I = 5, j = 5, p, q;
P = (I ++) + (I ++ );
Q = (++ J) + (++ J );
Printf ("% d, % d", p, q, I, j );
}
I + I ---> P, I + 1 --> I, I + 1 --> I, I + 1 --> I
J + 1-> J, J + 1-> J, J + 1-> J, J + J-> q int I = 5, j = 5, P, q;
P = (I ++) + (I ++ );
Q = (++ J) + (++ J );
In this program, P = (I ++) + (I ++) should be understood as three I additions, so the P value is 15. Then I auto-increment 1 three times is equivalent to adding 3 so the last value of I is 8. Q = (++ J) + (++ J) should be understood as Q first incrementing by 1 before calculation, since Q is incremented by 1 three times and the value is 8, the sum of the Three 8 values is 24, and the final value of J is still 8. An arithmetic expression is a combination of constants, variables, functions, and operators. An expression has a value and its type, which is equal to the value and type of the result obtained by the calculation expression. The expressions are evaluated in the order specified by the operator priority and associativity. A single constant, variable, and function can be considered as a special case of an expression.
Arithmetic expression
Is a formula that is connected by Arithmetic Operators and parentheses. The following is an example of an arithmetic expression:
A + B (A * 2)/C (x + r) * 8-(A + B)/7 + + I sin (x) + sin (y) (++ I)-(J ++) + (k --)
Value assignment operator and value assignment expression
Simple assignment operators and expressions. Simple assignment operators are marked as "= ". The formula connected by "=" is called a value assignment expression. The general form is: Variable = expression example:
X = a + B
W = sin (A) + sin (B)
Y = I ++ -- the function of the J value assignment expression is to calculate the value of the expression and then assign it to the variable on the left. The value assignment operator has the right combination. Therefore
A = B = c = 5
It can be understood
A = (B = (C = 5 ))
In other advanced languages, a value assignment is a statement called a value assignment statement. In C, "=" is defined as an operator to form a value assignment expression. Where an expression can appear, a value assignment expression can appear. For example, formula X = (a = 5) + (B = 8) is valid. It grants 5 to A and 8 to B, and then adds a and B to X. Therefore, X should be equal to 13.
A value assignment statement can also be formed in the C language. According to the C language, any expression without a semicolon is a statement. Therefore, for example, x = 8; a = B = c = 5; they are all value assignment statements. We have used them in many cases.
If the data types on both sides of the value assignment operator are different, the system automatically converts the data types, that is, the type on the right of the value assignment number is changed to the type on the left. The specific provisions are as follows:
1. The real type is given to the integer type, and the decimal part is removed. The preceding example 2.9 illustrates this situation.
2. the integer type is given to the real type, and the value remains unchanged, but it will be stored as a floating point, that is, add the decimal part (the decimal part value is 0 ).
3. the Bytes type is given to the integer type. Because the bytes type is one byte and the integer type is two bytes, the ASCII code value of the character is placed in the lower eight bits of the integer, and the height eight bits are 0.
4. The integer type is given to the character type, and only the lower eight characters are given to the character quantity.
Void main (){
Int A, B = 322;
Float X, Y = 8.88;
Char C1 = 'k', C2;
A = y;
X = B;
A = C1;
C2 = B;
Printf ("% d, % F, % d, % C", A, X, A, C2 );
}
Int A, B = 322;
Float X, Y = 8.88;
Char C1 = 'k', C2;
Printf ("% d, % F, % d, % C", A = Y, x = B, A = C1, C2 = B );
This example shows the type conversion rules in the preceding value assignment operation. A is an integer. If the value of X is 8.88, only the integer 8 is given. X is the real type, and the integer B value is 322, followed by a decimal part. The numeric type C1 is used to convert a to an integer. The integer B is assigned to C2 and the lower eight bits are used as the numeric type (the lower eight bits of B is 01000010, that is, decimal 66, according to the ASCII code corresponding to character B ).
Compound assignment operators and expressions
Add other binary operators before the value assignment operator "=" to form a composite value assignment operator. For example
+ =,-=, * =,/=, % =,> =, & =, ^ =, | =. The common form of composite value expression is: variable binary operator = expression, which is equivalent to variable = variable operator expression, for example: A + = 5 is equivalent to a = a + 5 x * = Y + 7. x = x * (Y + 7) R % = P is equivalent to r = R % P.
Composite value assignment operator may not be used to beginners, but it is very helpful for compilation, which can improve compilation efficiency and generate high quality target code. The comma operator and the comma expression are in
Comma Operator
In C, comma "," is also an operator called the comma operator. The function is to connect two expressions to form an expression called a comma expression.
Generally, expression 1 and expression 2 are used to evaluate the values of two expressions respectively, and the value of expression 2 is used as the value of the entire comma expression.
Void main (){
Int A = 2, B = 4, C = 6, X, Y;
X = a + B, y = B + C;
Printf ("Y = % d, x = % d", Y, X );
}
In a x example, Y is equal to the value of the entire comma expression, that is, the value of expression 2. x is the value of the first expression. The comma expression must be described as follows:
1. Expressions 1 and 2 in a comma expression can also be a comma expression. For example, expression 1 (expression 2, expression 3) forms a nested condition. Therefore, you can extend the comma expression to the following forms: expression 1, expression 2 ,... Expression n the value of the entire comma expression is equal to the value of expression n.
2. Use a comma expression in the program. Generally, You need to evaluate the values of each expression in the comma expression separately, but not necessarily the value of the whole comma expression.
3. Not all fields with commas form a comma expression. For example, in the variable description, the comma in the function parameter table is only used as the delimiter between variables.

This article is transferred from
Http://sefeng1982.bokee.com/5366860.html

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.