An expression may contain more than one operator. Which operator will execute first depends on its precedence. Precedence defines the sequence in which operators are to be applied on the operands, while evaluating the expressions involving more than one operator. Operators of same precedence are evaluated from left to right or right to left, depending upon the level. This is known as associativity property of an operator. A complete list of operator precedence as applicable in C language is presented below.
Ex: if(x==25 + 10 &&y<10)
In this example the highest priorities are given to the ( ). Then next the ‘+’ operator has got higher precedence as compare to && and relational operators (== and <). Therefore the addition of 25 and 10 will get calculate first i.e. 35. After this, statement will become:
if (x==35 && y<10)
C Operator Precedence and Associativity
Operator |
Description |
Associativity |
( ) |
Parentheses
(function call) Member
selection via object name Member
selection via pointer Postfix
increment/decrement |
left-to-right |
++ -- |
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 |
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 |
right-to-left |
, |
Comma
(separate expressions) |
left-to-right |