Order Of Operator Precedence In C++
C operator precedence is the most important thing in calculating arithmetic, relational and logical operations. So, now we need to discuss the order of operator precedence in C when all these operators come in a single expression. In this C tutorial for beginners, we are going to discuss their order of precedence.For example:
The expression like the ones below:
- 11> 1 || !9 && 5+5*2 && 6%8
- 2 > ‘A’ && 7*4 !true + (2 % 5 * 3 – 1 + 5)
How we are going to decide that which operator should be calculated first ,this will be done according to the operator precedence in c.
For this we need to know the c operator precedence order when they are together.
Operators Precedence
- ! , + , - (unary) first
- * , / , % second
- + , - third
- < , <= , > , >= fourth
- == , != fifth
- && sixth
- || seventh
- = (assignment operator) last
Note:
In C++, | and & are also operators. The behavior of these operators is different from && and ||.Examples:
1. !true && (20 >= 18)
First, we’ll evaluate !true which false and then (20>=18) which is true and then we’ll use && which will give us false.
2. (2*10>10 && 2*10 == 20) true
First we’ll calculate, 2*10 which is equal to 20 then 20>10 which is true and then 20 == 20 which is true and when we apply && on it we get true value.