C - Precedence and Associativity

C Programming

Share

In this tutorial we will learn about precedence and associativity in C programming language.

We evaluate expression based on the rules of precedence and associativity.

Precedence rules

Precedence rules decides the order in which different operators are applied.

Associativity rules

Associativity rules decides the order in which multiple occurences of the same level operator are applied.

Precedence and Associativity table is at the end of this tutorial.

Lets talk about the precedence of the arithmetic operators namely addition, subtraction, multiplication, division and modulus.

High priority is given to multiplication, division and modulus whereas, addition and subtraction gets the low priority.

High * / %
Low  + -

Evaluate the Expressions

In the following example we are given an arithmetic expression consisting of all the 5 operators namely addition, subtraction, multiplication, division and modulus.

We will solve the expression by first targeting high priority operators and then target the low priority operators.

Note! Arithmetic expressions are evaluated from left-to-right.

x = a2 - 3b + a / b

where, a = 4 and b = 2

First we will express the above formula using the arithmetic operators we use in C.

x = a * a - 3 * b + a / b

Now we will replace a and b with their respective values.

x = 4 * 4 - 3 * 2 + 4 / 2

Now we will solve the expression from left to right.

//find x
x = 4 * 4 - 3 * 2 + 4 / 2
  = (4 * 4) - 3 * 2 + 4 / 2      [* high priority operator]
  = 16 - 3 * 2 + 4 / 2
  = 16 - (3 * 2) + 4 / 2         [* high priority operator]
  = 16 - 6 + 4 / 2
  = 16 - 6 + (4 / 2)             [/ high priority operator]
  = 16 - 6 + 2
  = (16 - 6) + 2                 [- low priority operator]
  = 10 + 2
  = (10 + 2)                     [+ low priority operator]
  = 12

So, the final answer of the given expression is 12.

Lets write a C program for the above expression.

#include <stdio.h>
int main(void)
{
  //variables
  int x, a, b;
  
  //initialise
  a = 4;
  b = 2;
  
  //compute x
  x = a * a - 3 * b + a / b;
  
  //output
  printf("x = %d\n", x);
  
  printf("End of code\n");
  return 0;
}

Output

x = 12
End of code

Rules to evaluate given expressions

  • Expressions inside the ( ) parenthesis are evalutated first as they assume the highest priority.
  • In case of nested parenthesis, evaluate the innermost sub expression.
  • Operator with higher precedence gets higher priority.
  • If multiple operators are having same precedence then we apply associativity to determine which operator will be evaluated first.

Precedence and Associativity table

Operator Description Associativity Precedence
( )
[ ]
Function call
Array element reference
Left to Right 1
+
-
++
--
!
~
*
&
sizeof
(type)
Unary plus
Unary minus
Increment
Decrement
Logical negation
Ones complement
Pointer reference (indirection)
Address
Size of an object
Type cast (conversion)
Right to Left 2
*
/
%
Multiplication
Division
Modulus
Left to Right 3
+
-
Addition
Subtraction
Left to Right 4
<< 
>> 
Left shift
Right shift
Left to Right 5

<=

>=
Less than
Less than or equal to
Greater than
Greater than or equal to
Left to Right 6
==
!=
Equality
Inequality
Left to Right 7
& Bitwise AND Left to Right 8
^ Bitwise XOR Left to Right 9
| Bitwise OR Left to Right 10
&& Logical AND Left to Right 11
|| Logical OR Left to Right 12
?: Conditional expression Right to Left 13
=
*= /= %=
+= -= &=
^= |=
<<= >>=
Assignment operators Right to Left 14
, Comma operator Left to Right 15
Share