Operator precedence and Associativity
Each operator in C has a precedence associated with
it. The precedence is used to determine how an expression involving more than one operator is evaluated. There are distinct
levels of precedence and an operator may
belong to one of these levels. The operators of higher precedence are evaluated
first. The operators of same precedence are evaluated from
right to left or from left to right depending on the level. This is known as “associativity”
property of an operator. The
below table shows the operator precedence and their associativity.
Operator
|
Description
|
Associativity
|
()
[] . -> ++ -- |
Parentheses (function call) (see Note 1)
Brackets (array subscript) Member selection via object name Member selection via pointer Postfix increment/decrement (see Note 2) |
left-to-right
|
++
--
+ - ! ~ (type) * & sizeof |
Prefix increment/decrement
Unary plus/minus Logical negation/bitwise complement Cast (change type) Dereference Address Determine size in bytes |
right-to-left
|
*
/ %
|
Multiplication/division/modulus
|
left-to-right
|
+
-
|
Addition/subtraction
|
left-to-right
|
<<
>>
|
Bitwise shift left, Bitwise shift right
|
left-to-right
|
<
<=
> >= |
Relational less than/less than or equal
to
Relational greater than/greater than or equal to |
left-to-right
|
==
!=
|
Relational is equal to/is not equal to
|
left-to-right
|
&
|
Bitwise AND
|
left-to-right
|
^
|
Bitwise exclusive OR
|
left-to-right
|
|
|
Bitwise inclusive OR
|
left-to-right
|
&&
|
Logical AND
|
left-to-right
|
||
|
Logical OR
|
left-to-right
|
?:
|
Ternary conditional
|
right-to-left
|
=
+= -= *= /= %= &= ^= |= <<= >>= |
Assignment
Addition/subtraction assignment Multiplication/division assignment Modulus/bitwise AND assignment Bitwise exclusive/inclusive OR assignment Bitwise shift left/right assignment |
right-to-left
|
,
|
Comma (separate expressions)
|
left-to-right
|
No comments:
Post a Comment