C++

C++ Precedence Examples

In C++, when we are working with the operators, there is a sequence in which all the operations are performed. The operator precedence is utilized when working with multiple operators in our code’s expression. For example, while working with addition, subtraction, multiplication, and division operators, the multiplication and division operations are performed first from left to right as they have high precedence, and the addition and subtraction operations are performed next from left to right as they have low precedence than the multiplication and division. But if the expression only contains the addition and subtraction operators, the operation is simply performed from left to right. Here, we will show the table of the operator precedence and the examples of operator precedence.

Operator Precedence of Arithmetic Operations

Operation Operators Precedence
parentheses () These parentheses are operated.
multiplication, division, modulus *, /, % These operations are performed and their order is from left to right.
addition, subtraction +, – These operations are performed at the end and their sequence is from left to right.

Example 1:

Let’s begin our code by including the header file named “iostrem”. Many functions are declared in this “iostream” header file which we will use in our code. Below it, we type the “namespace std” and invoke the “main()” ahead. Here in “main()”, we declare the variables named “i”, “j”, and “k” with “int” and the values that we pass here are “10”, “37”, and “29”, respectively. We also declare the “r” variable where the result is saved.

After this, we type an expression using the “+, “/”, and “-” operators. As we previously explained, these operations are performed in a sequence. The expression that we placed here is “i + j / k – j” in which the division is performed first, and then the addition and subtraction operations are performed. It displays the outcome in integer values as we declare the “r” variable as the integer. In the following, we display the computed result using “cout”.

Code 1:

 #include <iostream>

using namespace std;

int main()

{

int i = 10, j = 37, k = 29, r;

r = i + j / k - j;

cout<<"The result of the given expression is = "<< r;

return 0;

}

Output:

The output is “-26” after performing all operations. This result is computed according to the operator precedence provided by the C++ language.

Example 2:

In this code, after including the header file and invoking “main()”, we initialize an “int” variable with the “val1” name and place the expression in which we type “9 + 18 * 7”. Here, it performs the multiplication first and then add “9” to the multiplicated value. In the following, we initialize “int val2” with “9 + (18 * 7)”.

Here, the parentheses are solved first. Then, the addition is performed. After this, we also initialize “int val3” with the “(9 + 18) * 7” expression. In this expression, the parentheses again are operated first and then the multiplication is performed. The expression values are the same but they provide unique outcomes because of the operator precedence. Now, we move ahead and print all the results which are stored in the “val1”, “val2”, and “val3” variables with the aid of “cout”.

Code 2:

#include <iostream>

using namespace std;

int main() {

  int val1 = 9 + 18 * 7;
  int val2 = 9 + (18 * 7);
  int val3 = (9 + 18) * 7;


  cout << "First result shows = " << val1 << endl;

  cout << "Second result shows = " << val2 << endl;

  cout << "Third result shows = " << val3 << endl;

  return 0;

}

Output:

The result that we get after computing the given expression is as follows. We can see that the third result is not the same as the other two, even though the values are the same. This is due to the operator precedence which is applied to the expression’s operator.

Example 3:

Here, we initialize different variables with different values, and the data type of all these variables is “int”. We have the integer variables “a”, “b”, “c”, and “d” with the values of “14”, “9”, “15”, and “29”, respectively. After this, we initialize “int R1” and the expression that we place here is “a + b) * c / d”. The parentheses are solved first. Then, the multiplication and division operations are performed. We also display the outcome below this. Then, we have “int R2” with the “a – b * (d + a) + c” expression. Here, it also solves the parentheses and then the subtraction and addition are performed according to the operator precedence. We also print its outcome and initialize “int R3” where the “b – d + c * a” expression is typed. In this expression, multiplication is performed first, then the subtraction and addition are performed. The result of this expression is also displayed in the following. Now, we have the last expression, “d + (a * b) / a”, whose result is stored in the “R4” variable. In this expression, the parentheses are solved first and then divided. At the end, addition is performed. The outcome of this expression is shown as follows:

Code 3:

#include <iostream>

using namespace std;

int main() {

  int a = 14;
  int b = 9;
  int c = 15;
  int d = 29;  
 int R1 = (a + b) * c / d;


 cout <<"(a + b) * c / d " << "gives = " << R1 << endl;

 int R2 = a - b * (d + a) + c;

 cout <<"a - b (d + a) + c " << "gives = " << R2 << endl;

 int R3 = b - d + c * a;

 cout <<"b - d + c * a " << "gives = " << R3 << endl;

 int R4 = d + (a * b) / a;

 cout <<"d + (a * b) / a " << "gives = " << R4 <<endl;

 return 0;

}

Output:

The following is the outcome of computing the previously mentioned expressions. We get this outcome because the operators in the expression have the operator precedence applied to them:

Example 4:

We initialize the “a1”, “b1”, “c1”, “d1”, “e1”, and “f1” variables here with the integer values of “64”, “90”, “11”, “27”, “34”, and “98”, respectively. We initialize the “int result1” here with the “a1 + b1 – c1) * d1 / f1” expression and the result is stored in the “result1” variable. Here, the parentheses are solved first. Then, the remaining operations are computed sequentially. Then, we print the “result1”. In the same way, we compute the expressions and store them in different variables. Then, we print all the results separately, showing how the operator precedence works.

Code 4:

#include <iostream>

using namespace std;

int main() {

  int a1 = 64;
  int b1 = 90;
  int c1 = 11;
  int d1 = 27;
  int e1 = 34;
  int f1 = 98;  
 int result1 = (a1 + b1 - c1) * d1 / f1;


 cout <<"The result 1 is " << result1 << endl;

 int result2 = a1 + (f1 * b1) / e1;

 cout <<"The result 2 is " << result2 << endl;

 int result3 = e1 + d1 + a1 - c1 * b1 / a1;

 cout <<"The result 3 is " << result3 << endl;

 int result4 = a1 - f1 + (b1- d1) * c1;

 cout <<"The result 4 is " << result4 <<endl;

 int result5 = (f1+ ( a1* (b1+ a1) - f1) * a1 + e1);

 cout <<"The result 5 is " << result5 <<endl;

 return 0;

}

Output:

The output of the given code is rendered here which shows the result of all the expressions that we entered previously. All expressions are computed by following the order precedence rule.

Conclusion

We looked at the “C++ precedence” here in which we deeply studied the order of the operators in which they are computed in the C++ programming.  We also showed the order of the operators in the form of a table and then performed numerous examples in which we learned how the expressions were solved according to the order precedence in C++ programming.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.