How do I remember the operator priority of C language?

Source: Internet
Author: User

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 )!

Related Article

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.