The precedence of operators determines which operator is executed first if there is more than one operator in an expression.
Let us consider an example:
int x = 5 - 17* 6;
In C, the precedence of *
is higher than -
and =
. Hence, 17 * 6
is evaluated first. Then the expression involving -
is evaluated as the precedence of -
is higher than that of =
.
Here’s a table of operators precedence from higher to lower. The property of associativity will be discussed shortly.
Operator | Meaning of operator | Associativity |
---|---|---|
() [] -> . | Functional call Array element reference Indirect member selection Direct member selection | Left to right |
! ~ + – ++ — & * sizeof (type) | Logical negation Bitwise(1 ‘s) complement Unary plus Unary minus Increment Decrement Dereference (Address) Pointer reference Returns the size of an object Typecast (conversion) | Right to left |
* / % | Multiply Divide Remainder | Left to right |
+ – | Binary plus(Addition) Binary minus(subtraction) | Left to right |
<< >> | Left shift Right shift | Left to right |
< <= > >= | Less than Less than or equal Greater than Greater than or equal | Left to right |
== != | Equal to Not equal to | Left to right |
& | Bitwise AND | Left to right |
^ | Bitwise exclusive OR | Left to right |
| | Bitwise OR | Left to right |
&& | Logical AND | Left to right |
|| | Logical OR | Left to right |
?: | Conditional Operator | Right to left |
= *= /= %= += -= &= ^= |= <<= >>= | Simple assignment Assign product Assign quotient Assign remainder Assign sum Assign difference Assign bitwise AND Assign bitwise XOR Assign bitwise OR Assign left shift Assign right shift | Right to left |
, | Separator of expressions | Left to right |