Operator precedence in the C language

Source: Internet
Author: User

The problem of operator precedence in C is generally not required, but if you can remember, it is very helpful to quickly understand code and programming; If you can quickly remember, why not?

If you like, it is recommended to first download my sorting operator Priority PDF document (http://download.csdn.net/detail/n289950578/7964209), which is more helpful for understanding the comparison.


1. Priority level:
In order to understand the convenience, the priority is divided into six categories, shorthand for "not 123 Fu":

Non- operator () [].->;

Single- mesh operation (!&* right-to-left);

Double- mesh operation (left-to-right, most complex, most important part);

Three mesh operation (X? A:b right-to-left);

Assignment Operation (*= right-to-left);

Tease Operation (, left-to-right);

In addition to binocular operations, each of the other classes represents a priority, merging a single binocular operation into a Limerick, as follows:

multiplication/Add/subtract/move left and right;

such as small and equal/unequal;

bit and/or XOR/bitwise OR;

logic with,/logical OR.

//take-up operation and except is the same principle, in order to facilitate the combination of poetry, omitted, understanding on the line


2, the combination of direction:

the so-called arithmetic combination direction, refers to the operator priority is the same time, the precedence of the left or the right.

all binocular and comma operations are left-to-right, the remaining monocular operations, the three-mesh operation and assignment from right to left; this does not have to be remembered at all, the following explains how to understand the line.

monocular : All monocular operations have only one operand, and the operation needs to form a model ( operator + operand ) (except for a++,a--), the operand must be determined before it is calculated. All operands are on the right from the surface to determine the operand, and then the operator, that is, from right to left; Here the monocular operation contains sizeof () and coercion type conversion ().

Binocular : When the binocular operation, must be ( operand + operator + operand ) model, before the operation must determine two operands, and then the operator to calculate, in accordance with the general direction , which is the order from left to right.

Three mesh : X? Y:z three mesh The order of operations is, X is computed first, if the result of X is true, then Y is executed, otherwise z is executed. When the so-called right-to-left direction is limited to multiple nested combinations, many people misunderstand the right-to-left process and interpret the calculation direction as z and y last X, as I have previously understood.

x?a:b?aa:bb = X?a: (B?AA:BB) instead of (X?A:B) AA:BB, (using VC6.0 single-step assembly to track C's conclusions). of course, in practice, in order to avoid misunderstanding, we must add brackets, no one will write this, so if you are not happy, the three-purpose directionality can also be remembered.

Assignment : It can be understood as Monocular , only if the operand is determined first , then the operator is combined to assign a value to the target object. Naturally, it is from right to left.

comma: comma operations do not have any special needs, in accordance with the normal sequence of the line, so from left to right order.


of course, everyone's understanding of the thinking is not the same, I just share my understanding of the method, if you can combine their own thinking to understand, is actually the fastest, Heavy on understanding . We are very welcome to share their understanding ideas! Reprint please indicate source: http://blog.csdn.net/n289950578/article/details/39502367

Operator precedence in the C language

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.