C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Precedence and Associativity

C Programming

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

OperatorDescriptionAssociativityPrecedence
( )
[ ]
Function call
Array element reference
Left to Right1
+
-
++
--
!
~
*
&
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 Left2
*
/
%
Multiplication
Division
Modulus
Left to Right3
+
-
Addition
Subtraction
Left to Right4
<< 
>> 
Left shift
Right shift
Left to Right5

<=

>=
Less than
Less than or equal to
Greater than
Greater than or equal to
Left to Right6
==
!=
Equality
Inequality
Left to Right7
&Bitwise ANDLeft to Right8
^Bitwise XORLeft to Right9
|Bitwise ORLeft to Right10
&&Logical ANDLeft to Right11
||Logical ORLeft to Right12
?:Conditional expressionRight to Left13
=
*= /= %=
+= -= &=
^= |=
<<= >>=
Assignment operatorsRight to Left14
,Comma operatorLeft to Right15