C + + operator precedence list __c++

Source: Internet
Author: User
Tags bitwise int size
C + + Operator precedence

The operators at the top of this list are evaluated.

Precedence Operator Description Example associativity
1 ::
Scoping operator
Class::age = 2;
None
2 ()
[]
->
.
++
--
Grouping operator
Array Access
member access from a pointer
member access from an object
Post-increment
Post-decrement
(A + B)/4;
ARRAY[4] = 2;
Ptr->age = 34;
Obj.age = 34;
for (i = 0; i < i++) ...
for (i = i > 0; i--) ...
Left to right
3 !
~
++
--
-
+
*
&
(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 = i > 0; i.) ...
int i =-1;
int i = +1;
data = *ptr;
Address = &obj;
int i = (int) floatnum;
int size = sizeof (floatnum);
Right to Left
4 ->*
.*
Member Pointer selector
Member Object Selector
Ptr->*var = 24;
Obj.*var = 24;
Left to right
5 *
/
%
Multiplication
Division
Modulus
int i = 2 * 4;
float F = 10/3;
int rem = 4% 3;
Left to right
6 +
-
Addition
Subtraction
int i = 2 + 3;
int i = 5-1;
Left to right
7 <<
>>
Bitwise SHIFT LEFT
Bitwise SHIFT Right
int flags = << 1;
int flags = >> 1;
Left to right
8 <
<=
>
>=
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
9 ==
!=
Comparison equal-to
Comparison not-equal-to
if (i = = 42) ...
if (i!= 42) ...
Left to right
10 & Bitwise AND Flags = flags & 42; Left to right
11 ^ Bitwise EXCLUSIVE OR Flags = flags ^ 42; Left to right
12 | Bitwise inclusive (normal) OR Flags = Flags | 42; Left to right
13 && Logical and if (Conditiona && conditionb) ... Left to right
14 || Logical OR if (Conditiona | | conditionb) ... Left to right
15 ? : Ternary conditional (IF-THEN-ELSE) int i = (a > B)? A:B; Right to Left
16 =
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=
Assignment operator
Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise AND ASSIGN
Bitwise exclusive OR and assign
Bitwise inclusive (normal) OR and assign
Bitwise SHIFT LEFT and assign
Bitwise SHIFT RIGHT 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
17 , Sequential evaluation operator for (i = 0, j = 0; I < i++, j + +) ... Left to right

One important aspect of C + + is related to operator precedence are the order of evaluation and the Order O F side effects in expressions. In some circumstances, the order in which things happen are not defined. For example, consider the following code:

float x = 1;
x = x/++x;

The value of x is not guaranteed to be consistent across different compilers, because it are not clear whether the computer Should evaluate the left or the right side of the division. Depending on which side is evaluated a, x could take a different value.

Furthermore, while ++x evaluates to x+1, the side effect of actually storing of this new value in X could happen at different times, resulting in different values for x.

The bottom line is this expressions like the one above are horribly ambiguous and should are avoided at all costs. When in doubt, a single ambiguous expression into multiple expressions to ensure this order of evaluation is COR Rect.

Reference Address: http://www.cppreference.com/operator_precedence.html

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.