Updated April 6, 2023
Introduction to Operators Precedence in C
Operator precedence in C tells you which operator is performed first, next, and so on in an expression with more than one operator with different precedence. This plays a crucial role while we are performing day to day arithmetic operations. Operator precedence in C is used to determine the order of the operators to calculate the accurate output. Parenthesis has the highest precedence whereas comma has the lowest precedence in C.
Why this Precedence Concept Come into Picture?
Let’s suppose we have addition, multiplication, division, subtraction, etc. in a single statement. If we do not have this precedence concept than 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 original outcome 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
For Always constant output we have to use precedence concept.
Same Precedence Operators in Single Statement
What Happens if the same Precedence Operators come in Single statement? If we get 2 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:
Examples of Operators Precedence in C
Below are the examples of Operators Precedence in C:
Example #1 – Parenthesis Precedence
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 read the entire output statement and start calculating based on precedence. As we discussed above first precedence 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 output as 245.
Example #2 – Precedence with Arithmetic Operators
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.
Example #3 – Same Addition Associativity with Functions
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:
Example #4
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.
Recommended Articles
This is a guide to Operators Precedence in C. Here we discuss the Introduction to Operators Precedence in C and its Table along with the different examples and code implementation. You can also go through our other suggested articles to learn more –