Operator Precedence in C++

Operator Precedence in C++
Techiio-author
Written by Sagar RabidasFebruary 7, 2022
6 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Operator Precedence in C++

Operator Precedence in C++

The operator precedence determines which operator is to be first evaluated and which subsequent in expression whilst one or greater operators are found in an expression. The alternative period which is associated with expression is operator associativity. Operator associativity determines the course of operator assessment of the same operator precedence, associativity can be left to proper to left.

Lets take an example: x = 10 + 22 / 2.

In the above example, the result is 21, not 16 because the ‘/’ operator has higher precedence than the ‘+’ operator.

Lets take an example: x = 10 -20+ 22 / 2.

In the above example, the result is 1, not 19 because the same as above ‘/’ operator has higher precedence than the ‘+’ operator and ‘+’ and ‘-‘ operator is of the same precedence and their associativity is left to right so the expression evaluates as –

  • x = 10 – 20+ 22 / 2
  • x=10 – 20+11
  • x=-10+11
  • x=1

So, Operators Precedence and operator Associativity are two characteristics of operators that specify the order of evaluation in an expression.

Example to Implement Operator Precedence in C++

Below are mentioned the examples:

Example #1

Code:

#include <iostream>
using namespace std;
int main()
{
// declare variables
int a = 10, b=22, c=2, x;
// expression
x= a + b / c;
// display result
cout<<"The result of the expression is = "<<x;
return 0;
}

Example #2

Next, we rewrite the above c++ code to understand the operator precedence overload with the following example:

Code:

#include<iostream>
using namespace std;
int main()
{
// declare variables
int a = 10, b=22, c=2, x;
// expression
x = ( a + b ) / c;
// display result
cout<<"The result of the expression is = "<<x;
return 0;
}

Example #3

Next, we write the c++ code for operator associativity with the following example:

Code:

#include <iostream>
using namespace std;
int main()
{
// declare variables
int a = 10, b=22, c=2, x;
// expression
x = a == b != c;
// display result
cout<<"The result of the first expression is = "<< x <<endl;
x = a == ( b != c );
cout<<"The result of the second expression is = "<<x <<endl;
return 0;
}

Output:

blogpost

Explanation: As in the above code the first expression operators == and != have the same precedence and the associativity is left to right so first == and then != operator performed. And in the second expression first != and then == operator performed. So first expression is solve as:

  1. x = a == b != c
  2. x = 10 == 22 != 2
  3. x=0 != 2
  4. x=1
  5. and second expression is solve as –
  6. x = a == (b != c)
  7. x = 10 == (22 != 2)
  8. x=10 == 1
  9. x=0

Example #4

Next, we write the c++ code for operator associativity with the following example:

Code:

#include <iostream>
using namespace std;
int main()
{
// declare variables
int a = 10, b=22, c=2, x;
// expression
x = a > b > c;
// display result
cout<<"The result of the first expression is = "<< x <<endl;
x = a < b < c;
cout<<"The result of the second expression is = "<<x <<endl;
return 0;
}

Output:

blogpost

Explanation: As in the above code, the first expression contains>operator whose associativity is left to right. Therefore the expression becomes ((a > b) > c), which becomes (0 > 2), so output is 0. And the second expression contains>operator whose associativity is left to right. Therefore the expression becomes ((a< b) < c), which becomes (1 < 2), so output is 1.

C
C++
Operator Precedence
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance