Hierarchy Of Operators / Precedence of Operators

Reading Time:- 1 min 55 sec

Reading Time:- 1 min 55 sec

#hierarchy-of-operators #operators-precedence #programming

Avatar

 BY 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.

search node.js on browser

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.


1st Precedence Operators

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




2nd Precedence Operators

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




3rd Precedence Operators

Precedence Operator Description Associativity
3 * / % Multiplication, Division and Modulo Left-to-right

4th Precedence Operators

Precedence Operator Description Associativity
4 + - Addition and Subtraction Left-to-right

5th Precedence Operators

Precedence Operator Description Associativity
5 <<>> Bitwise left shift and right shift Left-to-right

6th Precedence Operators

Precedence Operator Description Associativity
6 < <= For relational operators < and ≤ respectively Left-to-right
6 > >= For relational operators > and ≥ respectively Left-to-right


ADVERTISEMENT




7th Precedence Operators

Precedence Operator Description Associativity
7 == != = and ≠ respectively Left-to-right

8th Precedence Operators

Precedence Operator Description Associativity
8 & Bitwise AND Left-to-right

9th Precedence Operators

Precedence Operator Description Associativity
9 ^ Bitwise XOR Left-to-right

10th Precedence Operators

Precedence Operator Description Associativity
10 | Bitwise OR Left-to-right

11th Precedence Operators

Precedence Operator Description Associativity
11 && Logical AND Left-to-right

12th Precedence Operators

Precedence Operator Description Associativity
12 || Logical OR Left-to-right


ADVERTISEMENT




13th Precedence Operators

Precedence Operator Description Associativity
13 ?: Ternary Conditions Right-to-left

14th Precedence Operators

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

15th Precedence Operators

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.



COMMENTS


Search
Advertisement
Advertisement
Share With
Follow us on
Advertisement
Advertisement