Operator precedence in C language (code example)

藏色散人
Release: 2020-09-14 09:36:53
Original
91277 people have browsed it

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: << 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):

##< <= > >=Left to right== !=Left to right&Left to right ^Left to right##|##&&lefttoright||left to right?:right to left right to leftLeft to rightExample of operator precedence in C:
#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;}
Copy after login
Output:
OperatorAssociativity
() [] -> . - - Left to right
- ! ~ - - (type)* & sizeofright to left
* / % Left to right
-Left to right
<< >> ;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
Copy after login

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!

source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!