Operator Precedence in C++

Written by Sagar RabidasFebruary 7, 2022
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Sagar Rabidas

Software Developer

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:

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:

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
You must be Logged in to comment
Code Block
Sagar Rabidas
Software Developer
+8 more
300 Blog Posts
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17

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.