C Programming

# Operator Precedence in C

When we evaluate any expression that involves the usage of different operators in it, we are obliged to solve the expression to compute its values based on operator precedence. Precedence is the term for assigning a priority or weightage to some value/term, so the operator precedence assigns the priority to operators in an expression. To understand this, assume any expression, e.g., 8*3+2, and the answer to this expression could result in two different values, “26” and “40”, respectively. However, we will choose the result of the expression as “26”, since the multiplication “*” operator has more operator precedence as compared to the addition operator “+” hence, the expression will first compute the multiplication between 8 and 3 and the result will be then added with the 2. Many operators in the programming language C have more priority over the other operators.

## Procedure

Operator precedence is an important concept in numerical methods for solving an expression. We need this precedence to solve the expression. If we do not follow any rule, we won’t know what operator to solve first, and we may get a different answer every time. To avoid this confusion, we have standardized the priority to operators that tell what operator we must evaluate first in any expression. We will solve various examples with different operators in this article.

## Example 01

In the example, we will execute a program in the compiler Visual Studio C that will compute the numerical expression involving certain numerical operators. If we take an example expression as:

6*5-4+2

Without the operator precedence, we have two ways to solve this expression. One way is to solve this expression by evaluating the operators from left to right. If we do so, we will get the answer “28”. Another way to solve the expression is if we start evaluating the operators from right to left, we will compute the answer for this expression as “26”. The answers we have are different from each other, and they are also suspicious. How would we choose which answer is correct if these answers are not wrong? Let’s solve this example in the C compiler by developing a program for this operation. The visual studio C has default operator precedence for such expressions.

To implement this, we need to create a project and then declare the main function as “main()” with the return type “integer”. In the main function, initialize an integer with a variable name “a” and assign the previous numerical expression to it. Then display the result for this expression in the variable by calling the printf () method, which will take the format specifier for the integer variable “a” as “%i” and also the name of the variable “a” in its arguments. Here is the shortest program that you may execute:

#include <stdio.h>
int main()
{
int a = 6 * 5 - 4 + 2;
printf("%i", a);
return 0;
}

The output of the expression “a” will then be computed by this program as the “28”.

## Example 02

To know more about the operator precedence, we will solve an example for the expression involving the arithmetic operators and the bracket operators and learn how the C programming language gives precedence to multiple types of operators. The expression on which we are going to work in this example is as follows:

((3*8)+2)-2

The expression involves the two parentheses operators and the arithmetic operator “+”. To check out how the compiler for C will solve this expression, create a new project and declare a function named “main” with integer “int” as its return type. In this function, initialize a variable “x” with a data type integer. To this variable, assign the numerical expression, e.g., “int x=((3*8)+2)-2”, and display the variable in the output.

#include <stdio.h>
int main()
{
int x= ((3 * 8) + 2) - 2;
printf("%i",x);
return 0;
}

The output for the variable “x” declared in the previous example would be the numerical value “24”. We have this output since the C programming language precedes the parenthesis operator compared to the arithmetic operator “+”. So, the first operator evaluated in the expression was the parenthesis operator, which involved the multiplication of the two values, 3 and 8, that gave the value 24. Then compiler again solved the second parenthesis operator, which added the resulting value from the first parenthesis with numerical value 2. The answer to the second parenthesis is “26”. Then came the turn for the subtraction and “2” got subtracted from 26, which resulted in the answer of expression as 24.

## Example 03

For the third example, we will solve an expression involving the operators that are either same or have the same precedence. For such a case, let’s implement an example where we will declare the three variables x, y, and z, all having the same data type as an integer with the initialized values as 30, 20, and 10. We want to compare all three values as x>y>z. If the condition (x >y>z) becomes true, then the output will print “correct”. Otherwise, “false”. This example is implemented the following way:

#include <stdio.h>
int main()
{
int x = 30;
int y = 20;
int z = 10;

if (x > y > z)
printf("correct");
else
printf("FALSE");
return 0;
}

From the previous code, we can see that the expression has only comparator operators in it. In the C program, when the same type of operator exists in the expression, we simply compute the expression by evaluating the operator from left to right. That is the reason why the output has displayed the “correct” for the expression. If we move from left to right and compare the (x>y)>z, then this would be a correct statement for the values x=30, y=20=, and z=10.

## Conclusion

Operator precedence is the fundamental rule of assigning priorities to operators while dealing with numerical expressions in the programs written in C. This article highlights the precedence of the operators over the same and different operators for evaluation purposes. We have implemented three different examples with three different cases involving the operator precedence.