Home>Article>Backend Development> Operator precedence in C language (code example)

Operator precedence in C language (code example)

藏色散人
藏色散人 Original
2019-02-27 09:35:09 91385browse

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.

Operator precedence in C language (code example)

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: 5e334df76bd3c51cd28fde68dbef4be7> right shift operator.

  • Eight-level operators: 91eef5681cad6f0cc96f9adeb2931b45, >= 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):

#include main() { int a = 20; int b = 10; int c = 15; int d = 5; int e; e = (a + b) * c / d; // ( 30 * 15 ) / 5 printf("Value of (a + b) * c / d is : %d\n", e ); e = ((a + b) * c) / d; // (30 * 15 ) / 5 printf("Value of ((a + b) * c) / d is : %d\n" , e ); e = (a + b) * (c / d); // (30) * (15/5) printf("Value of (a + b) * (c / d) is : %d\n", e ); e = a + (b * c) / d; // 20 + (150/5) printf("Value of a + (b * c) / d is : %d\n" , e ); return 0;}
##7cb9091baf3e2c81106f6565e75575c8 >= Left to right == != Left to right & Left to right ^ Left to right ##| ##&& lefttoright || left to right ?: right to left right to left Left to right Example of operator precedence in C: Output:
Operator Associativity
() [] -> . - - Left to right
- ! ~ - - (type)* & sizeof right to left
* / % Left to right
- Left to right
10e3fdaca48eb0367c6d60dbc98f885d> ; Left to right
Left to right
##= = -= *= /= %=>>= <<= &= ^= |=
,
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!

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