Written by **Shuvhojit Deb**February 9, 2022

8 min read

C

Full Stack Developer

Operator priority in C tells you which driver is performed first, next, and so on in an expression with further than one driver with different priority. This plays a pivotal part while we're performing day-to-day computation operations. Operator priority in C is used to determine the order of the drivers to calculate the accurate affair. Gap has the loftiest priority whereas comma has the smallest priority inC.

Let’s suppose we have addition, multiplication, division, subtraction, etc. in a single statement. If we do not have this precedence concept then I have simply started calculating from left to right or right to left. But this is not correct because there is sometimes addition, multiplication, and division at starting of the statement, etc. So the user may or may not follow proper order for these operators. So to make a unique solution for calculating the original outcomes developers have introduced this precedence.

**Syntax:-**

5+2*10/1-3+([++4]-5*2-1);

**Code:-**

int out=1+4*10/2;

Calculate Left to Right then output 25.

Calculate Right to Left then output 21.

Now calculate based on precedence first *, followed by /,+.

4*10=40

40/2=20

20+1=21

What Happens if the same Precedence Operators come in a Single statement? If we get 2 the same precedences appearing in an expression, then it is said to be “Associativity”. Now, in this case, we can calculate this statement either from Left to right or right to left because both are having the same precedence.

**Syntax:-**

5+10+20; //Same output we will get if we calculate from left to right or right to left.

**Operator’s precedence table:-**

**Precedence with Variables:-**

Below are the examples of Operators Precedence in C:

**Code:-**

//used to include basice c library files

#include <stdio.h>

//main method for run the C application

int main()

{

//declaring variables

int a,b;

double output;

//Asking user to enter 2 numbers as input

printf("Please enter any 2 numbers \n");

//store 2 numbers in 2 variables

scanf("%d\n\t%d",&a,&b);

//assigning resultant of operators to a variable

output=(a-b)*(a+b*a)+a/b;

//displaying output

//first precedence given to (), followed by / and +

printf("output of %d and %d is = %lf ",a, b,output);

return 0;

}

**Output:-**

**Explanation:-**

- The first compiler reads the entire output statement and starts calculating based on precedence. As we discussed above first precedence is given to parenthesis(()). If inside parenthesis also has more than 2 parameters with different operators then inside parenthesis also followed precedence rule.
- (10-2)=8 and (10+2*10)=10+20=30
- 8*30=240
- 10/2=5
- 240+5=245
- Therefore we got an output of 245.

**Code:-**

//used to include basic c library files

#include <stdio.h>

//main method for run the C application

int main()

{

//declaring variables

int a,b;

double output;

//Asking user to enter 2 numbers as input

printf("Please enter any 2 numbers \n");

//store 2 numbers in 2 variables

scanf("%d\n\t%d",&a,&b);

//assigning resultant of operators to a variable

output=a+b*b-a/b%a;

//displaying output

//first precedence given to *, followed by /, %, + and -

printf("Output of %d and %d is =%lf ",a, b,output);

return 0;

}

**Output:-**

**Explanation:-**

- In the above example precedence order is *, /, %, + and -.
- b*b=>5*5=25
- a/b=>10/5=2
- a/b/%a=>a/b is already 2 so 2%5=2
- a+b*b=>10+25=35
- a+b*b-a/b%a=>35-2=33
- So therefore we got output as 33.

**Code:-**

//used to include basice c library files

#include <stdio.h>

//main method for run the C application

//defining methods

int function1();

int function2();

int function3();

int main()

{

//assigning resultant of operators to a variable

int output=function1()+function2()+function3();

//displaying output

//equal precedence operators so we can calculate in any order, get same output

printf("Output of associativity is= %d ",output);

return 0;

}

//method definination

int function1()

{

//declaring variables

int a;

//Asking user to enter 2 numbers as input

printf("Please enter any number \n");

//store a number in a variable

scanf("%d",&a)

return a;

}

//method definination

int function2()

{

//declaring variables

int a;

//Asking user to enter 2 numbers as input

printf("Please enter any number \n");

//store a number in a variable

scanf("%d",&a);

return a;

}

//method definination

int function3()

{

//declaring variables

int a;

//Asking user to enter 2 numbers as input

printf("Please enter any number \n");

//store a number in a variable

scanf("%d",&a);

return a;

}

**Output:-**

**Code:-**

//used to include basice c library files

#include <stdio.h>

//main method for run the C application

int main()

{

//declaring variables

int a;

//assign values to a variable

a=1,2,3,4,5,6,7,8;

//displaying output

//, has least precedence

printf("\n Output of a variable is = %d ",a);

return 0;

}

**Output:-**

**Explanation:-** From the above output, we got to know that the comma precedence least of all the operators in C.

C Language

Operator

Programming

Operators Precedence

Was this blog helpful?

Trending Technologies

15

Software40

DevOps46

Frontend Development24

Backend Development20

Server Administration17

Linux Administration26

Data Center24

Sentry24

Terraform23

Ansible83

Docker70

Penetration Testing16

Kubernetes21

NGINX20

JenkinsX17

Recommended Blogs

8

Recommended Threads

8

Anik Adhikary

Anik Adhikary

Anik Adhikary