Home  >  Article  >  What is the operator with the lowest precedence?

What is the operator with the lowest precedence?

烟雨青岚
烟雨青岚Original
2020-07-06 09:12:5627552browse

The operator with the lowest priority is: comma operator. The order in which expressions are combined depends on the priorities of the various operators in the expression; operators with higher priorities are combined first, and operators with lower priorities are combined later. Operators in the same line have the same priority.

What is the operator with the lowest precedence?

The operator with the lowest precedence is: the comma operator.

An expression may contain multiple data objects of different data types connected by different operators; since the expression has multiple operations, different combination orders may result in different results. The result may even be incorrect operation errors, because when an expression contains multiple operations, they must be combined in a certain order to ensure the rationality of the operations and the correctness and uniqueness of the results.

The priorities decrease from top to bottom, the top has the highest priority, comma operator has the lowest priority. The order in which an expression is combined depends on the precedence of the various operators in the expression. Operators with higher precedence are combined first, operators with lower precedence are combined later, and operators in the same line have the same priority.

Knowledge expansion

Priority

Priority has nothing to do with the order of evaluation. For example, a b && b*c, although * has the highest priority, the evaluation order of this expression is from left to right.

The priorities decrease from top to bottom, with the top one having the highest priority and the comma operator having the lowest priority.

With the same priority, combine according to the associativity. Most operators are associative from left to right, and only three precedences are associative from right to left. They are unary operators, conditional operators, and assignment operators.

Basic priorities need to be remembered:

Pointers are optimal, and monocular operations are better than binocular operations. Such as plus and minus signs.

Arithmetic operations first, then shift operations, and finally bit operations. Please pay special attention to: 1 << 3 2 & 7 is equivalent to (1 << (3 2))&7.

The logical operation is finally combined.

Operators

Operators are special functions that take one or more operands and return the corresponding value. Operands are values ​​used as input to an operator, usually literals, variables, or expressions. Operators can be unary, binary, or ternary. Unary operators have 1 operand, binary operators have 2 operands, and ternary operators have 3 operands.

Associativity

When the precedence of operators on both sides of an operand is the same, the order of operation of the expression is determined by the associativity of the operator. The concept of associativity is not found in other high-level languages. This is one of the characteristics of C language.

In the standard C language documentation, the associativity of operators is not very clearly explained. A perfect answer is: it is the arbiter, deciding which operator to execute first when several operators have the same priority. C language also specifies 34 operators with different associativity.

The combination direction of most operators is "from left to right", that is: first left and then right, also called "left associativity", for example, a-b c, there are two operators - and in the expression , and have the same priority, according to the direction of combining left and then right, first combine around the minus sign to perform the operation of a-b, and then combine around the plus sign to complete the operation (a-b) c.

In addition to left associativity, C language has three types of operators whose combination direction is from right to left, also called "right associativity", namely: unary operators, conditional operators, and assignment operators .

It is important to emphasize that whether it is left associativity or right associativity, it is for two adjacent operators with the same priority (not the operand in the expression). The operator determines the left and right The reference point of , first construct the operation with the previous operator (that is, the operator on the left side), which is left associative, and vice versa, it is right associative.

For more related knowledge, please visit PHP Chinese website! !

The above is the detailed content of What is the operator with the lowest precedence?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn