Talk about the operator priority [comprehensive text]

xiaoxiao2021-03-05  39

High Quality C / C Programming Guide

Dr. Lin Rui

Operator priority

There are dozens of operators in C / C language, and the priority of the operator is shown in Table 4-1. Note that the priority of one yuan operator - * is higher than the corresponding binary operator.

Priority operator combination law from high to low ranks () [] -> 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 right to left?: From right to left = = - = * = / =% = & = ^ = | = << = >> = from left to right

Priority and combination of table operators

l If the operators in the code line are more, determine the order in which the expression is determined, avoiding the default priority.

Because the Table 4-1 is more difficult, in order to prevent ambiguity and improve readability, the order of operation of the expression should be determined using parentheses. E.g:

Word = (high << 8) | LOW

IF ((A | B) && (A & C))

- Analysis: Wilderness - Published: 2004-7-24 22: 47: 00-- C language operators priority arithmetic operators (*, , -,%, /) relational operators (<<= >> = ==! =) Logical operator (! && ||) Assignment operator (=) ( =, - =, * =, / =,% =,) comma operator (,) conditional operator ( ? :) SizeOF bit operator (~, &, | ^) Priority arithmetic operator is higher than that of the bit shift operator is higher than the relational logic operator higher than the logical operator The conditional operator is higher than that the self-anti-assignment operator is equivalent to the arithmetic self-counter-assignment operator equivalent to the assignment operator.

C Chinese operator priority learning annotation

Chen Zhiwu chzg99@21cn.com filed the priority of the operator, and many of the people who know C will think: What is the difficulty? Not who is the priority! Who is it? In this way, the priority of the operator is not a big problem, but for an initiator, it is often easy to confuse and make mistakes. For a person who knows C , I believe it will be occasionally fell above, and I will continue to read it. The confusion brought about "priority high operation"

The priority of the C operator has a table, the operator is classified, and this table does not need to die hard, as long as there is a rough outline to be OK. For example, it should be remembered that the lowest priority is a comma operator, followed by an assignment operator, and then the second is a three-mean operator. The priority of the relationship operator is higher than the logical operator (excluding logical non-computational), the priority of the arithmetic operator is higher than the relational operator, like and - the priority is high than the front, but the highest It must be (). After you know these, you must have a guideline in your mind: the priority is high. Then look at an example: int x = 1, y = 0;

! x && x y && y;

The above statement appeared! , &&, , These four operators, then the problem is coming, who is it?

There is a classmate surname Cai stands up, operators are the highest priority here, the reason should be the first to count , not before calculating y, then count! X, calculate x y, finally They && get up. According to the idea of ​​Cai classmates, the result of the second step is 0 && x y && 1, because && is strictly calculated, there is a result of 0 results from both 0, so do not need to calculate x y, the result of the entire statement is: fake. According to the above-mentioned Cai classmates, the value of Y is 1, this is right?

A classmate who surnamed stood up and retorted. I think it should be calculated first! X, if the value is false, it does not need to be calculated, the last result is false. If the value is true, then the X Y is calculated, if the value is true, then calculate y, otherwise the final result is also false.

Cai Xongxiao did not serve, high-class students, you think and! Who is high priority? The high classmates replied, of course, high. Cai Xi Xu then asked, why should you calculate it first! The high classmate can't answer.

Yes, why do you want to count first!

Package determine the priority method

The high classmate is correct, why? Below I explain it to you. When multiple priority different operators, in order not confused, you can add parentheses, which is divided into hierarchical, the same level considering the combination problem, when it is determined to count, it is then Insert in the block. For example, the above example, we can add parentheses: from left to right, because! Is much higher than the && priority, there is (!!), But because && ratio low priority, there is (x y), is higher than the &&, so ( Y). This makes the entire form becomes: (! X) && (x y) && ( y), the outermost layer is two && operation, because &&'s combination is from left to right, so the above It can be seen as: A && B && C, first calculate A, then calculate B, and finally C. Due to x = 1, then! X is false, and there is no need to calculate again, and the value of the entire statement is false. After the execution is completed, the value of Y is different or 0. Therefore, when I can't understand who is before, when I don't know who, I will add a bracket to see the order. Let's make a parentheit practice: give statement c = a> b? A: b; plug. This statement has three operators: =,> ,?: How should I get parentheses?

First scheme: c = ((a> b)? A: b);

Second option: c = (a> (b? A: b));

Third solution: (c = a)> (b? A: b);

What should be the one? According to the high and low order of the operator priority,> The priority is higher than =, so it is impossible to enclose (c = a). And> The priority is higher than?: Operator. So it is impossible to enclose (b? A: b). Therefore, the first answer is correct.

Let's take another similar example:

INT i = 8, J = 4, K;

K = i

Suddenly, some people may have to calculate i and j. Here you may wish to take a look at it first. From the left to right, <级 高 = = =? 高??:::::::???????????????????????? :, So K = (i

So the priority of the operator must be careful, neither it is as difficult as it is, nor is it easy to imagine.

转载请注明原文地址:https://www.9cbs.com/read-32180.html

New Post(0)