Skip to content Skip to sidebar Skip to footer

Operator Precedence C++

When we move on in our life, we take some steps. Just like we walk up the staircase one step forward at a time, we do the same with the C++ programming language. You might also remember the BODMAS rule in mathematics, which told us how we are required to move forward with a numerical similarly; we got so many operators we recently got to know about even they need to use.

Operator Precedence tells you exactly how we should evaluate an expression. For example, in the expression (a + b) / c, the division operator has higher precedence than the addition. Hence we will evaluate the / (division). Then the result will be divided by c.
But what if we have the same precedence operators in an expression? Well, we solve that problem with the help of the operator associativity. It tells us which operator to use first and which to be last in case of the same precedence level.

Operators Precedence Table

This operator precedence table will help us evaluate the level of precedence of various core operators such as assignment operator, unary operator, comma operator, multiplication operator, logical operator, arithmetic operator, conditional operator, ternary operator, modulus operator, and many more. The language predefines these operator precedence rules as per the characteristics of operators.

PRECEDENCE CATEGORYOPERATOR PRECEDENCEOPERATOR ASSOCIATIVITY
Postfix() [] -> . ++ – –Left to Right
Unary+ – ! ~ ++ – – (type) * & sizeofRight to Left
Multiplicative* / %Left to Right
Additive+ –Left to Right
Shift<< >>Left to Right
Relational< <= > >=Left to Right
Equality== !=Left to Right
Bitwise AND&Left to Right
Bitwise XOR^Left to Right
Bitwise OR|Left to Right
Logical AND&&Left to Right
Logical OR||Left to Right
Conditional?:Right to Left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to Left
Comma,Left to Right

Operators Associativity

Operator associativity determines how expressions in C++ are evaluated. The most common form of associativity is left to right. This means that the operands of an operation are evaluated from left to right. For example, here is some code that calculates the sum of three numbers:

int x {8}; int y {4}; int z {9};

result = x + y * z;

In this case, the operations are performed in the following order:

  • y*z
  • x + (y*z)

This is how the precedence of operators decides the level of precedence as per the rules decided by C++.

CONCLUSION

In this blog, we’ve learned about the order computer uses the operators in an expression known as Operator Precedence and have taken another step in our journey of learning C++ programming. We got to know in what order the back end of our program calculates any of the expressions we wish it to calculate—hoping to learn many more related topics shortly.

As you all know, GeekonPeak has just begun and is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more comprehensive and easy-to-grasp courses for you (for free). Follow us on Facebook, Instagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂

Leave a comment

Sign Up to Our Newsletter

Be the first to know the latest updates