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/modulusleft-to-right
+  –Addition/subtractionleft-to-right
<<  >>Bitwise shift left, Bitwise shift rightleft-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 toleft-to-right
&Bitwise ANDleft-to-right
^Bitwise exclusive ORleft-to-right
|Bitwise inclusive ORleft-to-right
&&Logical ANDleft-to-right
| |Logical ORleft-to-right
? :Ternary conditionalright-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

  1. Comma Operator Has Lowest Precedence .
  2. Unary Operators are Operators having Highest Precedence.
  3. Sizeof is Operator not Function .
  4. Operators sharing Common Block in the Above Table have Equal Priority or Precedence .
  5. 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 OperatorA 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 OperatorA Binary operator is one which has two operand. e.g. plus , Minus .
AssociativityTheir 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 ;