'C++' Order of Precedence


Operators, Precedence, and Associativit

Operators are tokens that are used to combine values in expressions. When evaluating an expression, C uses precedence and associativity rules to determine the order in which operators are applied. In the table below, the C operators are listed in order of precedence with highest precedence at the top. The rows of the table define precedence groups. Two operators in the same group have the same precedence. Higher precedence operators are applied before lower precedence operators.

Associativity rules are only used to determine the order of evaluation for two operators that are in the same group. Most groups use left to right associativity, which means that in an expression with operators in the same precedence group, the operators are applied in left-to-right order. The order is reversed for right to left associativity.
Level
Operator Name
operator group
associativity type of operation
1
Function call
( )
left to right selectors
1
Array subscripting
[ ]
left to right  
1
Component Selection
->
left to right  
1
Indirect Component Selection
.
left to right  
2
Unary +
+
right to left unary
2
Unary -
-
right to left  
2
Increment
++
right to left  
2
Decrement
--
right to left  
2
Logical NOT
!
right to left  
2
Complement
~
right to left  
2
Indirection
*
right to left  
2
Address of
&
right to left  
2
Cast
(type)
right to left  
2
Size of type or object
sizeof
right to left  
3
Multiplication
*
left to right multiplicative
3
Division
/
left to right  
3
Modulus (remainder)
%
left to right  
4
Addition
+
left to right additive
4
Subtraction
-
left to right  
5
Left shift
<<
left to right bitwise
5
Right shift
>>
left to right  
6
Less than
<
left to right comparison
6
Less than or equal to
<=
left to right  
6
Greater than
>
left to right  
6
Greater than or equal to
>=
left to right  
7
Equal to
= =
left to right comparison
7
Not equal to
!=
left to right  
8
Bitwise AND
&
left to right bitise
9
Bitwise exclusive OR
^
left to right bitwise
10
Bitwise inclusive OR
|
left to right bitwise
11
Logical AND
&&
left to right boolean
12
Logical inclusive OR
||
left to right boolean
13
Conditional operator
?:
right to left conditional
14
Simple assignment
=
right to left assignment
14
Assign sum
+=
right to left  
14
Assign difference
-=
right to left  
14
Assign product
*=
right to left  
14
Assign division
/=
right to left  
14
Assign remainder
%=
right to left  
14
Assign left shift
<<=
right to left  
14
Assign right shift
>>=
right to left  
14
Assign AND
&=
right to left  
14
Assign inclusive OR
|=
right to left  
14
Assign exclusive OR
^=
right to left  
15
Comma
,
left to right comma

Assignment Expressions

An assignment expression has the following form.
    variable-expression assignment-operator expression
The variable expression can be just the name of a variable, or it can be an expression that selects a variable using pointers, array indices, or struct selectors. The value type of the right-hand-side expression must be compatible with the variable type.

An assignment expression is most often used for its side effect: it changes the value of the variable selected by the variable expression to the value of the expression on the right-hand side. The value of the assignment expression is the value that is assigned to the selected variable.

In most common assignment expressions, the assignment operator is =. Then the assignment expression has the following form.

    variable-expression = expression

Some of the C arithmetic and bitwise operators can be combined with = to form assignment operators. For example, the += assignment operator indicates that the right-hand side should be added to the variable, and the *= assignment operator indicates that the right-hand side should be multiplied into the variable.


Page URL: http://www.d.umn.edu /~gshute/C/expressions.html
Page Author: Gary Shute
Last Modified: Monday, 20-Sep-1999 05:45:25 CDT
Comments to: gshute@d.umn.edu