What is the Use of Associativity & Operator Precedence in C Programming?

Do you know, what is Operators in C? Some symbols that carry out a specific mathematical/ logical computation on the given operands. So you can say that operators are one of the most important components of any programming language, and without them, programming languages are of no major use.

Let us understand what operators are by taking an example.

X = Y*Z Here, * is the multiplication operator that operates on the given operands Y and Z.

What is Operator Precedence in C? 

While using the C operators to carry out various operations, various operators are used in an expression as shown in the below example.  This can be explained by an example.

X = (b-a) + (c-a)

In the above example, the various operators of the same priority are used. You can solve this by using the BODMAS rule followed in mathematics where each operator has a preset priority and operations are carried out based on the priorities.

What is the Use of Associativity & Operator Precedence in C Programming

So operator precedence in C is used to know which operator will be considered first among a given set of operators.

The precedence of operators in C can be explained using an operator precedence table in C. But before we get to the operator precedence table, let us first understand the meaning of operator associativity in C.

Associativity of Operators in C 

Operator associativity in c is used when two operators have the same precedence appearing in an expression.

There are two types of operator associativity: Left to Right and Right to Left. This will later be explained in detail with the help of an example. First, let us take a look at Operator Precedence Table.

Operator Precedence Table 

Operator precedence in c

Point to Remember:  The precedence of Arithmetic Operators is: Multiplication operators (multiplication, division, modulus) and then additive operators (+ and -).

Associativity Continued 

1) Whenever there are two or more operators in an expression that have the same precedence, only then use the associativity of operators.

2) The precedence of Arithmetic Operators is Multiplication operators (division, multiplication, modulus) and then additive operators (+ and -).

Also Read: What is the use of Storage Class in C?

Sample Code to Understand Operator Precedence in C 

#include <stdio.h>

int main()

{

int x = 30;

int y = 20;

int z = 25;

int t = 15;

int u;

u = (x+y) * z / t;

printf(“Value of (x + y)*c/d is: %d\n”, u);

u = ((x+y)*z)/t;

printf(“Value of ((x + y)*z)/t is: %d\n” , u);

u = (x+y)*(z/t);

printf(“Value of (x+y)*(z/t) is: %d\n”, u);

u = x+(y*z)/t;

printf(” Output is : %d\n” , u);

return 0;

}

Output

Value of (x + y)*c/d is: 83

Value of ((x + y)*z)/t is: 83

Value of (x+y)*(z/t) is: 50

Output is: 63

Some Examples for C Operator Precedence

1) 

#include<stdio.h>

int main()

{

float n;

n =-2+3-6*7/6%12;

printf (“n=%f”,n);

}

Output

n=-6.000000

Also Read: How to compare two strings Without using Strcmp in C?

Explanation

Here, the operations are being performed according to their precedences.  Here is a step-by-step order of operations.

  1. a) -6*7 = -42
  2. b) -42/6 = -7
  3. c) -7%12 = -7
  4. d) 3 + (-7) = -4
  5. e) -2 -4 = -6

-6 is the answer.

2)

#include<stdio.h>

int main()

{

float n;

n=3%4+5%4;

printf (“n=%f”,n);

}

Output

n=4.000000

3)

#include<stdio.h>

int main()

{

float n;

n =-2*-3%-5/-4;

printf (“n=%f”,n);

}

Output

n=0.000000

Also Read: How to implement Sizeof and Strlen in C?

4)

#include<stdio.h>

int main()

{

float n=2.5;

int m=4;

n= m/2+n*8/m-m+n/3;

printf (“n=%f”,n);

}

Output

n=3.833333

5)

#include<stdio.h>

int main()

{

int a=4, b=5;

flaot n;

float x=5.5;

n=b*b/a+a/3*x+3+x;

printf (“n=%f”,n);

}

Output

n=20.000000

6) 

#include<stdio.h>

int main()

{

int a=3, b=4;

float c;

c=a*b/5-13/13+13/4*17/b;

printf(“c=%f”,c);

return 0;

}

Output

c=13.000000

7) 

#include<stdio.h>

int main()

{

int  x=-3- -3;

int y=-3- -(-3);

printf(“x=%d y=%d”, x, y);

return 0;

}

Output

x=0 y=-6

8) 

#include<stdio.h>

int main()

{

int x=200, y=300,z;

z=(x==200||y>300);

printf(“z=%f”, z);

return 0;

}

Output

z=0.000000

Practice Question of Post and Pre-Increment/ Decrement Operators

1)

#include<stdio.h>
int main()
{
int a=5;
int b= a++ + a++;
printf(“Value of a=%d\n”, a);
printf(“Value of b=%d\n”, b);
return 0;
}

Output

Value of a=7

Value of b=11

Also Read: How Many types of Pointers are Available in C Programming?

Explanation

A post-increment operator has been used in this program.

The value of variable a of type integer has been set as 5.

Another variable b is equal to the sum of a++ and a++.

When asked to print the value of ‘a’, the compiler prints 7 instead of 5, because a has been incremented twice in the program.

When asked to print the value of b, the compiler prints 11.

This is because, b = a++ + a++. The first time, the post-increment operator is applied on a++, the value changes to 5 but does not get assigned to the variable. So the variable still holds the value as 5.

The next time the post-increment operator is used, the value is 6 (it should have been 7, but new incrimination has not been considered yet). So b becomes 5 + 6 = 11.

2)

#include<stdio.h>
int main()
{
int a=50;
int b= a++ + a++ + a++;
printf(“Value of a=%d\n”, a);
printf(“Value of b=%d\n”, b);
return 0;
}

Output

Value of a=53

Value of b=153

Explanation

A post-increment operator has been used in this program.

The value of variable a of type integer has been set as 50.

Another variable b is equal to the sum of a++, a++, and a++. When asked to print the value of ‘a’, the compiler prints 53 instead of 50, because a has been incremented thrice in the program. When asked to print the value of b, the compiler prints 153. This is because, b = a++ + a++ + a++.

Also Read: What is the use of Double Pointer in C?

The first time the post-increment operator is applied on a++, the value changes to 51 but does not get assigned to the variable. So the variable still holds the value as 50.

The next time the post-increment operator is used, the value is 51 (it should have been 52, but new incrementation has not been considered yet).

The third time the post-increment operator is used, the value of a is 52. So b becomes 50 + 51 + 52 = 153.

3)

#include<stdio.h>
int main()
{
int a=5;
int b= a++ + a;
printf(“Value of a=%d\n”, a);
printf(“Value of b=%d\n”, b);
return 0;
}

Output

Value of a=6

Value of b=11

Explanation

A post-increment operator has been used in this program. The value of variable a of type integer has been set as 5. Another variable b is equal to the sum of a++ and a.

When asked to print the value of ‘a’, the compiler prints 6 instead of 5, because a has been incremented once in the program.

Also Read: How Dangling Pointer in C affects your programming?

When asked to print the value of b, the compiler prints 11. This is because, b = a++ + a.

The first time the post-increment operator is applied on a++, the value changes to 5 but does not get assigned to the variable. So the variable still holds the value of 5.

The next time a is written, its value becomes 6 (because of the previously used post-increment operator). So b becomes 5 + 6 = 11.

4)

#include<stdio.h>
int main()
{
int a=5;
int b= ++a + ++a;
printf(“Value of a=%d\n”, a);
printf(“Value of b=%d\n”, b);
return 0;
}

Output

Value of a=7

Value of b=14

Explanation

A pre-increment operator has been used in this program. The value of variable a of type integer has been set as 5. Another variable b is equal to the sum of ++a and ++a. When asked to print the value of ‘a’, the compiler prints 7 instead of 5, because a has been incremented twice in the program.

When asked to print the value of b, the compiler prints 14. This is because, b = ++a + ++a. The first time the pre-increment operator is applied on ‘a’, the value changes to 6and get assigned to the variable. So the variable now holds the value as 6.

Also Read: How many types of preprocessor directives are available in C Language?

The next time ++a is written, its value becomes 8 (because of the previously used pre-increment operator, and since the pre-increment operator is used again). So b becomes 6 + 8 = 14.

5)

#include<stdio.h>
int main()
{
int a=5;
int b= ++a + ++a + a + ++a;
printf(“Value of a=%d\n”, a);
printf(“Value of b=%d\n”, b);
return 0;
}

Output

Value of a=8

Value of b=29

Explanation

A pre-increment operator has been used in this program. The value of variable a of type integer has been set as 5.

Another variable b is equal to the sum of ++a, ++a, a and ++a. When asked to print the value of ‘a’, the compiler prints 8 instead of 5, because a has been incremented thrice in the program.

Also Read: Memory Layout of C Program

When asked to print the value of b, the compiler prints 29. This is because, b = ++a + ++a + a++a. The first time the pre-increment operator is applied on ‘a’, the value changes to 6 and gets assigned to the variable. So the variable now holds the value as 6.

The next time ++a is written, its value becomes 7 (because of the previously used pre-increment operator, and since the pre-increment operator is used again). The next time a is written, the value is still 7. The last time ++a is used, the value becomes 9. So b becomes 6 + 7 + 7 + 9 = 29.

6)

#include<stdio.h>
int main()
{
int a=5;
int b= ++a + a++ + a++ + ++a;
printf(“Value of a=%d\n”, a);
printf(“Value of b=%d\n”, b);
return 0;
}

Output

Value of a=9

Value of b=29

Explanation

Both Pre increment operators and Post increment operators have been used in this program.

The value of variable a of type integer has been set as 5. Another variable b is equal to the sum of ++a, a++, a++ and ++a.

When asked to print the value of ‘a’, the compiler prints 9 instead of 5, because a has been incremented four times in the program. When asked to print the value of b, the compiler prints 29. This is because, b = ++a + a++ + a++ + ++a.

The first time the pre-increment operator is applied on ‘a’, the value changes to 6and get assigned to the variable. So the variable now holds the value as 6.

The next time a++ is written, its value remains 6 because of post-increment. The next time a++ is used, the value becomes 7. The last time ++a is used, the value becomes 10. So b becomes 6 + 6 + 7 + 10 = 29.

7)

#include<stdio.h>
int main()
{
int a=5;
int b= a– + a–;
printf(“Value of a=%d\n”, a);
printf(“Value of b=%d\n”, b);
return 0;
}

Output

Value of a=3

Value of b=9

Explanation

A post decrement operator has been used in this program.

The value of variable a of type integer has been set as 5. Another variable b is equal to the sum of a– and a–. When asked to print the value of ‘a’, the compiler prints 3 instead of 5, because a has been decremented twice in the program.

When asked to print the value of b, the compiler prints 9. This is because, b = a– + a–.

The first time post decrement operator is applied on ‘a’, the value changes to 4but do not get assigned to the variable. So the variable still holds the value as 5.

The next time a– is written, its value remains 4 because of post decrement. So b becomes 5 + 4  = 29.

8)

#include<stdio.h>
int main()
{
int a=5;
printf(“Value of a++=%d\n”, a++);
printf(“Value of –a=%d\n”, –a);
printf(“Value of ++a=%d\n”, ++a);
printf(“Value of a++=%d\n”, a++);
printf(“value of a=%d\n”, a);
return 0;
}

Output:

Value of a++=5

Value of –a=5

Value of ++a=6

Value of a++=6

value of a=7

Explanation

Both the increment operators and decrement operators have been used in this program.

The value of variable a of type integer has been set as 5. When asked to print the value of a++, the compiler prints 5, because a has post incremented so the value has not been assigned to the variable yet.

When asked to print the value of –‘a’, the compiler prints 5. This is because the value of a was post incremented to 6, then pre decremented that causes its value to decrease by 1, and since it is pre decrement, the value gets assigned to the variable.

The next time ++a is written, its value becomes 6 because of pre-increment.

The next time a++ is used, the value becomes 7 but is printed as 6 because it is post-increment.

The last time compiler is asked to print the value of a, which has not become 7. So 7 is printed on the screen.

Hello, My Name is Abhinav. I am an Author in the Education Category of Trickyedu. I have Done My Engineering in Computer Science from DIT University. I have a good command on Science, Programming Language, and microprocessors. So, I choose this platform to share my knowledge and experience.

Leave a Comment