Written by **Sagar Rabidas**February 7, 2022

6 min read

C++

Software Developer

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.

Below are mentioned the examples:

#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;

}

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

#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;

}

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

#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;

}

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

- x = a == b != c
- x = 10 == 22 != 2
- x=0 != 2
- x=1
- and second expression is solve as –
- x = a == (b != c)
- x = 10 == (22 != 2)
- x=10 == 1
- x=0

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

#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;

}

**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

Was this blog helpful?

Trending Technologies

15

Software40

DevOps46

Frontend Development24

Backend Development20

Server Administration17

Linux Administration26

Data Center24

Sentry24

Terraform23

Ansible83

Docker70

Penetration Testing16

Kubernetes21

NGINX20

JenkinsX17

Recommended Blogs

8

Recommended Threads

8

Anik Adhikary

Anik Adhikary

Anik Adhikary