C++ Operator Precedence

‮th‬tps://www.theitroad.com

Operator precedence in C++ determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first, followed by operators with lower precedence. When operators have the same precedence, their evaluation order is determined by their associativity.

The following is a list of the C++ operators, arranged in order of decreasing precedence:

  1. Scope resolution operator (::)
  2. Postfix increment (++) and decrement (--)
  3. Function call operator ()
  4. Member selection operator (.)
  5. Member selection operator (->)
  6. Type cast operator (dynamic_cast, static_cast, reinterpret_cast, const_cast)
  7. Unary operators (+, -, !, ~, ++, --, *, &, sizeof, new, delete)
  8. Multiplicative operators (*, /, %)
  9. Additive operators (+, -)
  10. Shift operators (<<, >>)
  11. Relational operators (<, >, <=, >=)
  12. Equality operators (==, !=)
  13. Bitwise AND operator (&)
  14. Bitwise XOR operator (^)
  15. Bitwise OR operator (|)
  16. Logical AND operator (&&)
  17. Logical OR operator (||)
  18. Conditional operator (?:)
  19. Assignment operators (=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=)
  20. Comma operator (,)

It is important to note that parentheses can be used to change the order of evaluation in an expression. Expressions within parentheses are evaluated first, regardless of operator precedence.

In summary, operator precedence determines the order in which operators are evaluated in an expression. Understanding operator precedence is important to write correct and readable code.