C ++ operator priority list and learning Annotation

Source: Internet
Author: User
Tags arithmetic operators
Precedence Operator Description Example Associativity
1 ()
[]
->
.
::
++
--
Grouping Operator
Array access
Member access from a pointer
Member access from an object
Scoping Operator
Post-Increment
Post-Decrement
(A + B)/4;
Array [4] = 2;
PTR-> age = 34;
OBJ. Age = 34;
Class: age = 2;
For (I = 0; I <10; I ++ )...
For (I = 10; I> 0; I --)...
Left to right
2 !
~
++
--
-
+
*
&
(Type)
Sizeof
Logical Negation
Bitwise Complement
Pre-Increment
Pre-Decrement
Unary minus
Unary plus
Dereference
Address
Cast to a given type
Return size in bytes
If (! Done )...
Flags = ~ Flags;
For (I = 0; I <10; ++ I )...
For (I = 10; I> 0; -- I )...
Int I =-1;
Int I = + 1;
Data = * PTR;
Address = & OBJ;
Int I = (INT) floatnum;
Int size = sizeof (floatnum );
Right to left
3 -> *
.*
Member pointer Selector
Member pointer Selector
PTR-> * Var = 24;
OBJ. * Var = 24;
Left to right
4 *
/
%
Multiplication
Division
Modulus
Int I = 2*4;
Float F = 10/3;
Int REM = 4% 3;
Left to right
5 +
-
Addition
Subtraction
Int I = 2 + 3;
Int I = 5-1;
Left to right
6 <
>
Bitwise shift left
Bitwise shift right
Int flags = 33 <1;
Int flags = 33> 1;
Left to right
7 <
<=
>
> =
Comparison less-
Comparison less-than-or-Equal-
Comparison greater-
Comparison geater-than-or-Equal-
If (I <42 )...
If (I <= 42 )...
If (I> 42 )...
If (I> = 42 )...
Left to right
8 =
! =
Comparison equal-
Comparison not-Equal-
If (I = 42 )...
If (I! = 42 )...
Left to right
9 & Bitwise AND Flags = flags & 42; Left to right
10 ^ Bitwise exclusive or Flags = flags ^ 42; Left to right
11 | Bitwise random sive (normal) or Flags = flags | 42; Left to right
12 && Logical and If (conditiona & conditionb )... Left to right
13 | Logical or If (conditiona | conditionb )... Left to right
14 ? : Ternary conditional (if-then-else) Int I = (A> B )? A: B; Right to left
15 =
+ =
-=
* =
/=
% =
& =
^ =
| =
<=
>>=
Assignment operator
Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise AND and assign
Bitwise exclusive or and assign
Bitwise random sive (normal) or and assign
Bitwise shift left and assign
Bitwise shift right and assign
Int A = B;
A + = 3;
B-= 4;
A * = 5;
A/= 2;
A % = 3;
Flags & = new_flags;
Flags ^ = new_flags;
Flags | = new_flags;
Flags <= 2;
Flags> = 2;
Right to left
16 , Sequential evaluation Operator For (I = 0, j = 0; I <10; I ++, J ++ )... Left to right

 

C ++Learning annotation of operator priority in

 

When we mention the operator priority, many people who know C ++ will think: what is the difficulty? It's not who has a higher priority. Indeed, the priority of operators is not a big problem, but it is often easy for beginners to confuse and make mistakes. For a person who understands C ++, I believe that he will occasionally fall down and continue to read it if he does not believe it.

 

Confusions caused by "Advanced Computing with higher priority"

 

In C ++, the operator priority has a table that classifies operators in the table. This table does not need to be memorized, as long as there is a rough outline, it will be OK. For example, remember that the lowest priority is the comma operator, followed by the value assignment operator, followed by the three-object operator. The priority of Relational operators is higher than that of logical operators (excluding logical non-operational operations). The priority of arithmetic operators is higher than that of Relational operators, but the highest is. After knowing this, you must have a rule in your mind: First calculation with a higher priority. Here is an example:

Int x = 1, y = 0;

! X & X + Y & + Y;

The above statement appears! , &, +, And ++ operators. The question is, who is the first?

A student surnamed Cai stood up and said that the ++ operator has the highest priority in this field. Therefore, the ++ operator should first calculate ++ and then calculate ++ y! X, calculate x + y, and get them. According to Cai's ideas, the result of step 2 is 0 & X + Y & 1. Because & is a strict calculation, one of the 0 results is both 0, therefore, we do not need to calculate X + Y. The result of the entire statement is false. According to Cai, the value of Y should be 1 after execution, right?

A classmate surnamed Gao stood up and retorted, "I think we should calculate it first! X. If the value is false, the calculation is not required. The final result is false. If the value is true, X + Y is calculated. Similarly, if the value is true, ++ y is calculated. Otherwise, the final result is false.

Cai said, "You think ++ and! Who has a higher priority? Gao replied, it is of course ++ High. Cai asked why he had to calculate it first! What about it? Students cannot answer the question.

Yes, why! What about it?

 

Method for determining priority with parentheses

 

Gao Tong is correct. Why? I will explain it to you below. When multiple operators with different priorities are combined, brackets can be added to avoid confusion. In this way, the layers are separated. The integration of the same layers is considered, when you confirm, calculate the block first and then go deep into it. For example, in the above example, we can add parentheses: from left to right, because! Higher than &, so there are (! X), and because & + has a lower priority, there is (x + y), while ++ has a higher priority than &, so (++ Y ). In this way, the entire formula becomes :(! X) & (x + y) & (++ y), the outermost layer is two & Operations. Since the combination of & is from left to right, therefore, the above formula can be regarded as: A & B & C. Calculate a first, then B, and finally c. Because x = 1, then! X is false, and the value of the entire statement is false. After execution, the value of Y is not changed, or 0.

Therefore, when you are confused about who you are first and then who you are, add a bracket to check the order. The following is an exercise with parentheses: Give the statement C = A> B? A: B; parentheses. This statement has three operators: =,>, and ,? : How should we add parentheses?

Solution 1: c = (A> B )? A: B );

Solution 2: c = (A> (B? A: B ));

Solution 3: (C = A)> (B? A: B );

What is it? According to the order of operator priority,> priority is higher than =, so it is impossible to enclose (C =. > The priority is higher? : Operator. So it is impossible? A: B. Therefore, the first answer is correct.

 

Let's look at a similar example:

Int I = 8, j = 4, K;

K = I <j? ++ I: ++ J;

Suddenly, some people may need to calculate ++ I and ++ J. We recommend that you add parentheses. From left to right, the priority of <is higher than = and higher? :, So k = (I <j )? ++ I: ++ J. Continue to the right, because ++ is higher? :, So k = (I <j )? (++ I) :( ++ J), which is equivalent to k =? B: C. Calculate the value of a first. If it is true, the value is B, which calculates ++ I. If it is false, the value is C, which calculates ++ J. After the entire statement is executed, the value of K is 5, the value of I is 8, and the value of J is 5.

Therefore, you must be careful about the priority of operators, which is neither as difficult as you think nor as easy as you think.

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.