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:-
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:-
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.