C language operator priority, operator priority

Source: Internet
Author: User
Tags bitwise operators

C language operator priority, operator priority

 

Priority

Operator

Name or meaning

Usage

Integration direction

Description

1

[]

Array subscript

Array name [constant expression]

Left to right

--

()

Parentheses

(Expression)/function name (parameter table)

--

.

Member selection (object)

Object. member name

--

->

Member selection (pointer)

Object Pointer-> member name

--

 

2

-

Negative Operator

-Expression

Right to left

Single Object Operator

~

Bitwise Inverse Operator

~ Expression

++

Auto-increment operator

++ Variable name/variable name ++

--

Auto-subtraction Operator

-- Variable name/variable name --

*

Value Operator

* Pointer variable

&

Bitwise operators

& Variable name

!

Logical non-Operator

! Expression

(Type)

Forced type conversion

(Data Type) Expression

--

Sizeof

Length Operator

Sizeof (expression)

--

 

3

/

Division

Expressions/Expressions

Left to right

Binary Operators

*

Multiplication

Expression * Expression

%

Remainder (Modulo)

Integer expression % integer expression

4

+

Add

Expression + expression

Left to right

Binary Operators

-

Subtraction

Expression-expression

5

<

Move left

Variable <expression

Left to right

Binary Operators

>

Right Shift

Variable> Expression

 

6

>

Greater

Expression> Expression

Left to right

Binary Operators

> =

Greater than or equal

Expression> = expression

<

Less

Expression <expression

<=

Less than or equal

Expression <= expression

7

=

Equal

Expression = expression

Left to right

Binary Operators

! =

Not equal

Expression! = Expression

 

8

&

Bitwise AND

Expressions & Expressions

Left to right

Binary Operators

9

^

Bitwise OR

Expression ^ expression

Left to right

Binary Operators

10

|

By bit or

Expression | expression

Left to right

Binary Operators

11

&&

Logic and

Expressions & Expressions

Left to right

Binary Operators

12

|

Logic or

Expression | expression

Left to right

Binary Operators

 

13

? :

Conditional Operators

Expression 1?

Expression 2: expression 3

Right to left

Three-object Operator

 

14

=

Value assignment operator

Variable = expression

Right to left

--

/=

Assignment After Division

Variable/= expression

--

* =

Assign value after Multiplication

Variable * = expression

--

% =

Assign value after modulo operation

Variable % = expression

--

+ =

Add and assign values

Variable + = expression

--

-=

Value after subtraction

Variable-= expression

--

<=

Value after left shift

Variable <= expression

--

>>=

Value after right shift

Variable> = expression

--

& =

Bitwise AND postvalue

Variable & = expression

--

^ =

Value Based on bitwise XOR

Variable ^ = expression

--

| =

Value by bit or after

Variable | = expression

--

 

15

,

Comma Operator

Expressions, expressions ,...

Left to right

--

Note:
For operators with the same priority, the operation order is determined by the combination direction.
Note :! > Arithmetic Operators> Relational operators >&>||> value assignment operators

 

Char s [] = "012345678 ";

Char * p = s;

 

1. ++ * p: PPoint to S [0], and add S [0] and 1 as the expression value, so the output is 1. At this time, S [0] = '1'
2. ++ (* p): P also points to S [0] (S [0] The current value is 1), and adds S [0] and 1 as the expression value, so the output is 2. S [0] = '2'
3. * (++ p): p points to S [1], then takes the value of S [1] as the value of the expression, and outputs '1'
4. * ++ p: P points to S [2], then takes the value of S [2] As the expression value, and outputs '2'
5. (* p) ++: P still points to S [2]. Take the value of S [2] as the value of the expression, so '2' is output ', then the value of S [2] is added with 1, S [2] = 3
6. * (p ++): P still points to S [2] (present value is 3). Take the value of S [2] as the value of the expression, so output '3 ', then P points to S [3].
7. * p ++: P points to S [3] and takes the value of S [3] as the value of the expression. Therefore, output '3' and P points to S [4].


 

* P ++ extracts the value of * p first, and then asks p ++
(* P) ++ extracts the value of * p first, so that this value can be ++
* (P ++) is to extract the value of * p first, so that p ++
Therefore, * p ++ is equivalent to * (P ++)
In addition, printf runs from right to left. In addition, the right ++ operation is performed after the entire operation expression is completed, and the execution order of the right ++ operation is from left to right.

When the left ++ encounters a variable, it will immediately increase the value of the variable.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.