C + + operator precedence

Source: Internet
Author: User
Tags bitwise

Precedence Operator Description Example associativity
1 ()
[]
-
.
::
++
--
Grouping operator
Array Access
Member access from a pointer
Member access from a object
Scoping operator
Post-increment
Post-decrement
(A + B)/4;
ARRAY[4] = 2;
Ptr->age = 34;
Obj.age = 34;
Class::age = 2;
for (i = 0; i <; i++) ...
for (i = ten; i > 0; i--) ...
Left to right
2 !
~
++
--
-
+
*
&
(type)
sizeof
Logical negation
Bitwise complement
Pre-increment
Pre-decrement
unary minus
Unary Plus
Dereference
Address of
Cast to a given type
Return size in bytes
if (!done) ...
Flags = ~flags;
for (i = 0; i <; ++i) ...
for (i = ten; i > 0; i.) ...
int i =-1;
int i = +1;
data = *ptr;
Address = &obj;
int i = (int) floatnum;
int size = sizeof (floatnum);
Right-left
3 ->*
.*
Member pointer Selector
Member pointer Selector
Ptr->*var = 24;
Obj.*var = 24;
Left to right
4 *
/
%
Multiplication
Division
Modulus
int i = 2 * 4;
float F = 10/3;
int rem = 4% 3;
Left to right
5 +
-
Addition
Subtraction
int i = 2 + 3;
int i = 5-1;
Left to right
6 <<
>>
Bitwise Shift Left
Bitwise Shift Right
int flags = << 1;
int flags = >> 1;
Left to right
7 <
<=
>
>=
Comparison Less-than
Comparison less-than-or-equal-to
Comparison Greater-than
Comparison geater-than-or-equal-to
if (I < 42) ...
if (i <= 42) ...
if (i > 42) ...
if (i >= 42) ...
Left to right
8 ==
!=
Comparison equal-to
Comparison not-equal-to
if (i = = 42) ...
if (i! = 42) ...
Left to right
9 & Bitwise and Flags = flags & 42; Left to right
10 ^ Bitwise Exclusive OR Flags = flags ^ 42; Left to right
11 | Bitwise inclusive (normal) OR Flags = Flags | 42; Left to right
12 && Logical and if (Conditiona && conditionb) ... Left to right
13 || Logical OR if (Conditiona | | conditionb) ... Left to right
14 ternary Conditional (If-then-else) int i = (a > B)? a:b; right to left
15 =
+ =
-=
*=
/=
% =
&=
^=
|=
<<=
>>=
assignment operator
Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise and and assign
Bitwis e exclusive OR and assign
Bitwise inclusive (normal) or and assign
Bitwise shift left and assign
Bitwise shift RI Ght and assign
int a = B;
A + = 3;
B-= 4;
a *= 5;
a/= 2;
a%= 3;
Flags &= new_flags;
Flags ^= new_flags;
Flags |= new_flags;
Flags <<= 2;
Flags >>= 2;
right to left
16 , Sequential evaluation operator for (i = 0, j = 0; i <; i++, J + +) ... Left to right

Here is the Chinese version of

Priority level

Operator

Name or meaning

Use form

Combination direction

Description

1

[]

Array subscript

array name [constant expression]

Left to right

()

Parentheses

(expression)/function name (formal parameter list)

.

Member selection (object)

Object. Member name

-

Member selection (pointer)

Object pointers, member names

2

-

Minus sign operator

-expression

Right to Left

Monocular operator

Type

Forcing type conversions

(data type) expression

++

Self-increment operator

+ + variable name/variable name + +

Monocular operator

--

Self-decrement operator

--Variable name/variable name--

Monocular operator

*

Value operator

* Pointer variable

Monocular operator

&

Fetch address operator

& Variable Name

Monocular operator

!

Logical non-operator

! An expression

Monocular operator

~

Bitwise inverse operator

~ Expression

Monocular operator

sizeof

Length operator

sizeof (expression)

3

/

Except

An expression/expression

Left to right

Binocular operator

*

By

Expression-expression *

Binocular operator

%

Remainder (modulo)

Integer expression/integer expression

Binocular operator

4

+

Add

An expression + an expression

Left to right

Binocular operator

-

Reducing

Expression-expression

Binocular operator

5

<<

Move left

Variables << expressions

Left to right

Binocular operator

>>

Move right

Variables >> expressions

Binocular operator

6

>

Greater than

Expressions > expressions

Left to right

Binocular operator

>=

Greater than or equal

Expression->= expression

Binocular operator

<

Less than

Expressions < expressions

Binocular operator

<=

Less than or equal

Expression-<= expression

Binocular operator

7

==

Equals

Expression-= = Expression

Left to right

Binocular operator

!=

Not equal to

Expression! = Expression

Binocular operator

8

&

Bitwise-AND

Expressions & Expressions

Left to right

Binocular operator

9

^

Bitwise XOR OR

An expression ^ expression

Left to right

Binocular operator

10

|

Bitwise OR

Expression-expression

Left to right

Binocular operator

11

&&

Logic and

Expressions && Expressions

Left to right

Binocular operator

12

||

Logical OR

An expression | | An expression

Left to right

Binocular operator

13

?:

Conditional operators

Expression 1? Expression 2: Expression 3

Right to Left

Trinocular operator

14

=

Assignment operators

variable = expression

Right to Left

/=

Assign value after addition

Variable-/= expression

*=

Multiply post-Assign value

Variable-*= expression

%=

Assign value after modulo

Variable-%= expression

+=

Add after Assignment

Variable + = expression

-=

Reduced-value Assignment

Variable-= expression

<<=

Assign value after left shift

Variable-<<= expression

>>=

Assign value after right shift

Variable->>= expression

&=

Bitwise AND post-assigned values

Variable-&= expression

^=

Bitwise XOR or post-assignment

Variable-^= expression

|=

Bitwise OR post-assigned value

Variable-|= expression

15

,

Comma operator

expression, expression,...

Left to right

Left-to-right sequential operation

Description

Operators of the same precedence, the order of operations is determined by the binding direction.

Simple note is:! > Arithmetic operators > Relational operators > && > | | > Assignment Operators


C + + operator precedence

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.