Reading Time:- 1 min 55 sec

Reading Time:- 1 min 55 sec

#hierarchy-of-operators #operators-precedence #programmingBY TEJAS

When you try to solve a mathematical expression, you may encounter different results. Just look at this simple example.

Suppose you are given the expression A / B * C. To solve it by one person A / (B * C) or another person will solve it by (A / B) * C this way. But notice that the result of both A / (B * C) and (A / B) * C expression will be different.

ADVERTISEMENT

Suppose you put the values A = 5, B = 4, C = 6 in the above expression then the value of A / (B * C) will be 0, and the value of (A / B) * C will be 6. but the value of (A / B) * C = 6 is the correct value.

Hierarchy-Of-Operators

Just as algebra uses the "BODMAS" rule to handle such expressions, similarly programming uses a rule of the hierarchy of operators.

The hierarchy of operators is a very simple concept.

This is a rule used in a programming language to solve arithmetic expressions in which the priorities of the operators are already set, this is called the hierarchy of operators.

Hierarchy is also called precedence. Hierarchy is the rule which sets which operator to be solved first or which operator to be solved second.

ADVERTISEMENT

Below are the priorities of all the operators. The precedence of operators is the same in most programming languages.

Precedence | Operator | Description | Associativity |
---|---|---|---|

1 | ++ -- | Suffix/postfix increment and decrement | Left-to-right |

1 | () | Function Call | Left-to-right |

1 | [] | Array Subscripting | Left-to-right |

1 | . | Structure and union member access | Left-to-right |

1 | -> | Structure and union member access through pointer | Left-to-right |

1 | (type){list} | Compound Literals | Left-to-right |

ADVERTISEMENT

Precedence | Operator | Description | Associativity |
---|---|---|---|

2 | ++ -- | Prefix increment and decrement | Right-to-left |

2 | + - | Unary plus and minus | Right-to-left |

2 | (type) | Cast | Right-to-left |

2 | ! ~ | Logical NOT and bitwise NOT | Right-to-left |

2 | & | Address-of | Right-to-left |

2 | * | Indirection | Right-to-left |

2 | sizeof | Size-of | Right-to-left |

2 | _Alignof | Alignment requirement | Right-to-left |

ADVERTISEMENT

Precedence | Operator | Description | Associativity |
---|---|---|---|

3 | * / % | Multiplication, Division and Modulo | Left-to-right |

Precedence | Operator | Description | Associativity |
---|---|---|---|

4 | + - | Addition and Subtraction | Left-to-right |

Precedence | Operator | Description | Associativity |
---|---|---|---|

5 | <<>> | Bitwise left shift and right shift | Left-to-right |

Precedence | Operator | Description | Associativity |
---|---|---|---|

6 | < <= | For relational operators < and ≤ respectively | Left-to-right |

6 | > >= | For relational operators > and ≥ respectively | Left-to-right |

ADVERTISEMENT

Precedence | Operator | Description | Associativity |
---|---|---|---|

7 | == != | = and ≠ respectively | Left-to-right |

Precedence | Operator | Description | Associativity |
---|---|---|---|

8 | & | Bitwise AND | Left-to-right |

Precedence | Operator | Description | Associativity |
---|---|---|---|

9 | ^ | Bitwise XOR | Left-to-right |

Precedence | Operator | Description | Associativity |
---|---|---|---|

10 | | | Bitwise OR | Left-to-right |

Precedence | Operator | Description | Associativity |
---|---|---|---|

11 | && | Logical AND | Left-to-right |

Precedence | Operator | Description | Associativity |
---|---|---|---|

12 | || | Logical OR | Left-to-right |

ADVERTISEMENT

Precedence | Operator | Description | Associativity |
---|---|---|---|

13 | ?: | Ternary Conditions | Right-to-left |

Precedence | Operator | Description | Associativity |
---|---|---|---|

14 | = | Simple assignment | Right-to-left |

14 | += -= | Assignment by sum and difference | Right-to-left |

14 | *= /= %= | Assignment by product, quotient, and remainder | Right-to-left |

14 | <<=>>= | Assignment by bitwise left shift and right shift | Right-to-left |

14 | &= ^= |= | Assignment by bitwise AND, XOR, and OR | Right-to-left |

Precedence | Operator | Description | Associativity |
---|---|---|---|

15 | , | Comma | Left-to-right |

I think this article helped you to learn hierarchy of operators.

If you liked this article please comment down below and if you want to upgrade this article contact us.

Thank you.

- Linear / Sequential Search Technique
- Inheritance In Java: A Simple But Complete Guide
- Constructor In Java: A Simple But Complete Guide
- A Begineer's Guide to Data Visualization in Python
- What is TensorFlow? A Beginner's Guide to Know about it.
- Top Reasons Why Python is so Popular in programming Languages ?

COMMENTS