Often write the program, encountered the problem of operator precedence, I am ashamed of IS, check the number of books quite a lot of ... Mercilessly, summed up under. But also to combine a lot of programming practice to go into the mind.
1, the first thing you can never forget is that the comma operator level is the lowest, there is no controversy in the (), and manually set the highest priority, first of all in parentheses.
2, non-artificial, is four, function call (), [] array subscript, dot operator, indirect, operator. They are the highest priority, from left to right. Combination of memory
3, and at least know that all the monocular operators have the same level of precedence, remember that it is all. And remember everything from right to left.
For example: Sign +i and-I, self-increment ++i and i--, take the ground value &a, take the value *a, take the type size operator sizeof (int), logical non! A, the logical bitwise negation ~A, they are all single-purpose operators, and at the same time appear in the expression, the order of operations is right-to-left.
4, know a rule, the priority of the overall operator is monocular > Binocular > Three mesh > Assignment (including compound assignment), remove a few special.
With the premise, the next climax!
With the Netizen summed up a jingle with my improvement, and usually combined with practice will be able to remember with less effort! "Zhang Fei calculated Wei Yan, Guan Yu, or flirt with women," What do you mean?
The following is the order of precedence from high to the bottom, and the binding is judged by common sense:
Zhang Fei = logical Non-(fly) operator! (reminding of the aforementioned monocular, in addition to those four, Monocular is the highest level, all right to left, take sizeof () to remember the reminder is right to left
Calculation = calculation (calculation) operator is followed by +-*/% (the Order of elementary mathematics teaching, first calculate */%, then add and subtract, from left to right, the primary school is such, unchanged)
Wei Yan = shift (WEI) operator then is the shift operator "," said 1000 0011 who moved 4 bits, that must be left to right in order to calculate, 10000011<4, equivalent to Division
Guan Yu = Guan (Guan Yu) the system and the bitwise AND (BCE) operators are then the relational operators, here Note the point: = = and! The final calculation, and the combination of left to right, in fact, according to common sense is also true.
Then the bitwise AND operator & (~ is Monocular)
or = bit or second bit or |, of course, if you write a program more, naturally remember the bit XOR ^, do not have to clip in the middle, learned the composition principle to know left to right order
This can be a turn on logic with && (feather), then logic or | | (or), the combination of the thought of short-circuit operation, you know is left to right.
Flirt = (tune) conditional operator? : (Trinocular) Finally, the conditional operator, which is the low-level three-mesh operator
Women = Assignment and compound assignment Finally, assignment operators = and compound assignment operators, such as-=,+=,*=,/=,%=,&=,*=, =, =,|=,^= ..., are all right-to-left binding
Attention:
1. If there are many operators in the program segment, it is best to enclose them in parentheses, not too confident.
2, C + + operators, added a few, but the original C, or applicable.
3. Logical operators && and | | And! Computes the logical truth of the entire expression, while the bitwise operator of the & and ^ and ~ and |, computes the binary number of each bit
4, this jingle to follow the premise of that 4, the Jinan:
The man who is the tallest is enclosed in parentheses
The highest natural is four (), [], point,->, left to right.
Comma lowest
In level three, the Monocular is the highest, and the monocular are right to left. Second binocular and three mesh.
Summary of operator Precedence for A/C + + series