Sunday 6 October 2013

Order Of Operator Precedence In C++

 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

  1.   ! , + , - (unary)                            first
  2.       * , / , %                                 second
  3.         + , -                                      third
  4.   < , <= , > , >=                            fourth
  5.       == , !=                                    fifth
  6.         &&                                       sixth
  7.          ||                                       seventh
  8.   = (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.

Final words:

C operator precedence  is very important while we are dealing with arithmetic , relational and logical operations.Keeping this fact in mind shapes in c has designed this C tutorial for beginners for its users which will help them out to understand how to solve such expressions.

0 comments:

Post a Comment