When reading the code, someone suddenly wrote no parentheses, not to say that his code is not good,
But they all write to a certain extent, so the basics must not be forgotten.
Original] How do I remember the operator priority of C language?
I have been learning C language for a while, and it is difficult to remember the operator priority of C language, especially for beginners! Maybe you will say it doesn't matter if you don't remember it. use parentheses to change the priority. However, in many cases, the dependency on parentheses can easily lead to poor program readability. Of course, I am not opposed to parentheses, but advocate proper. In short, it is easier to remember and read other people's programs. I recently reviewed the priorities and found that there are still rules to follow. I would like to share with you and hope to help you! The operator priority table of C language is given first:
CLanguage operator priority table (descending order of priority from top to bottom)
Operator |
Associativity |
() []->. |
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. Image () []->. and so on are of course the most priority. In fact, they are not any operators at all. Second, apart from the above four operators, the next is the single-object operator, that is! ~ + ---(Type) * & sizeof. Remember that their order is from right to left! In fact, it is easy to understand the combination of instances, such as I ++. Third, it is followed by the binary operator, which is also the most confusing place in the C language operator priority. It is not so terrible, and you will find it after careful observation. In binary operators, Arithmetic Operators have the highest priority, followed by shift operators, followed by Relational operators, and then logical operators. However, in Relational operators, <<=>>==! =. In addition, in logical operators, the Operation has a higher priority than OR, and the exclusive or operation is between them. Similarly, you can draw an analogy between the priorities of & |. Fourth, after the binary operator, it is the three-object operator. There is nothing to say. Fifth, then the value assignment operator. You may mix the priority of the value assignment operator with that of the three object operator. It doesn't matter. I think you must have written such a statement (if not, remember it !) : Max = (a> B )? A: B. From this statement, it is not difficult to remember why the value assignment operator has a lower priority than the three-object operator! Sixth, the comma operator separates sub-statements (I think this is a bit inaccurate, but I think you will understand what I mean). Naturally, it has the lowest priority, I think this should not be confusing. To sum up, sort by operator priority from high to low: single object operator-> binary operator-> three object operator-> value assignment operator-> comma operator is particularly in binary operator, from high to low by operator priority: Arithmetic Operators-> Shift Operators-> Relational operators (where = and! = Low priority)-> logical operators (bitwise AND-> bitwise OR-> logic and-> logic or )!