Operator priority in C language: 1. The first-level operators have identifiers and constants; 2. The second-level operators have array subscript operations and function calls; 3. The third-level operators have prefixes of Increment and prefix decrement; 4. The fourth-level operator forces the expression to become a specified type; 5. The fifth-level operator is the multiplication operator, etc.
For example, x = 7 3 * 2; Here, x is assigned the value 13 instead of 20 because the precedence of operator * is higher than, So it first multiplies 3*2 and then adds it to 7.
Here, the highest precedence operator appears at the top of the table and the lowest precedence operator appears at the bottom of the table. In an expression, operators with higher precedence are evaluated first.
First-level operators: identifiers, constants, string literals, priority promotion expressions are executed first.
Secondary operators: array subscript operation (expression), function call (argument-expression-list), member access (identifier, -> identifier), suffix increment ( i), suffix decrement (i--), composite initialization (initializer-list).
Third-level operators: prefix increment (i), prefix decrement (--i), unary conversion expression (get address &, withdraw *, plus sign , negative sign -, bit inversion ~ logical no!), find the type length (sizeof unary-expression).
Fourth-level operator: Forces the expression to become the type specified by type-name (type-name) cast-expression.
Five-level operator: " * " multiplication operator.
Six-level operators: " " addition operator.
Seven-level operators: << left shift operator; >> right shift operator.
Eight-level operators: <, <=, >, >= relational operators.
Nine-level operators: "==" is equal to the operator; "!=" is not equal to the operator.
Tenth level operator: "&" bitwise AND operator.
Eleventh level operator: "∧" bitwise XOR operator.
Twelve-level operator: "|" bitwise OR operator.
Thirteenth level operator: "&&" logical AND operator.
Fourteenth level operator: "||" logical OR operator.
Fifteenth-level operators: ? :Conditional operator.
The operator precedence in C language is as follows (from high to low):
Operator | Associativity |
() [] -> . - - | Left to right |
- ! ~ - - (type)* & sizeof | right to left |
* / % | Left to right |
- | Left to right |
<< >> ; | Left to right |
Left to right | |
Left to right | |
Left to right | |
Left to right | |
Left to right | ##&& |
|| | |
?: | |
##= = -= *= /= %=>>= <<= &= ^= |= | |
, | |
Example of operator precedence in C: |
Value of (a + b) * c / d is : 90 Value of ((a + b) * c) / d is : 90 Value of (a + b) * (c / d) is : 90 Value of a + (b * c) / d is : 50
Related recommendations: "
C Tutorial"
This article is an introduction to operator priority in C language. I hope it will be useful to friends who need it. Helped!
The above is the detailed content of Operator precedence in C language (code example). For more information, please follow other related articles on the PHP Chinese website!