C Programming operator precedence & associativity
C Programming supports wide range of operators. While Solving the Expression we must follow some rules
While solving the expression [ a + b *c ] , we should first perform Multiplication Operation and then Addition, similarly in order to solve such complicated expression you should have hands on Operator Precedence and Associativity of Operators.
Operator precedence & associativity table
Operator precedence & associativity are listed in the following table and this table is summarized in decreasing Order of priority i.e topmost operator has highest priority and bottommost operator has Lowest Priority.
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 (convert value to temporary value of type) Dereference Address (of operand) Determine size in bytes on this implementation | 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 |
Summary of operator precedence
- Comma Operator Has Lowest Precedence .
- Unary Operators are Operators having Highest Precedence.
- Sizeof is Operator not Function .
- Operators sharing Common Block in the Above Table have Equal Priority or Precedence .
- While Solving Expression , Equal Priority Operators are handled on the basis of FIFO [ First in First Out ] i.e Operator Coming First is handled First.
Important definitions
Unary Operator | A unary operation is an operation with only one operand, i.e. an operation with a single input . A unary operator is one which has only one operand. e.g. post / pre increment operator |
Binary Operator | A Binary operator is one which has two operand. e.g. plus , Minus . |
Associativity | Their associativity indicates in what order operators of equal precedence in an expression are applied |
Precedence | Priority Of Operator |
Examples : operator precedence & associativity
We have listed out some of the examples based on operator precedence & associativity. Examples will give you clear picture how to use operator precedence & associativity table chart while solving the expression
Example 1 : Simple use of precedence chart
#include<stdio.h> int main() { int num1 = 10, num2 = 20; int result; result = num1 * 2 + num2; printf("\nResult is : %d", result); return (0); }
consider the simple expression used in above program and refer operator precedence & associativity table chart
result = num1 * 2 + num2;
In this case multiplication operator will have higher priority than addition and assignment operator so multiplication will be evaluated firstly.
result = num1 * 2 + num2; result = 10 * 2 + 20; result = 20 + 20; result = 40;
Example 2 : Use of associativity
In above example none of the operator has equal priority. In the below example some operators are having same priority.
result = num1 * 2 + num2 * 2 ;
In the above expression we have overall 3 operators i.e. 2 multiplication, 1 addition and 1 assignment operator.
Now refer operator precedence & associativity table (block 3) which clearly tells that associativity is from left to right so we will give priority to multiplication operator on the left side
result = num1 * 2 + num2 * 2 ; result = 10 * 2 + 20 * 2 ; result = 20 + 20 * 2 ; result = 20 + 40 ; result = 60 ;