C programming - operator precedence

w‮w‬w.theitroad.com

In C programming, operator precedence determines the order in which operators are evaluated in an expression. When an expression contains multiple operators, the operator with higher precedence is evaluated first.

Here is the precedence order of some of the most common operators in C programming, from highest to lowest precedence:

  1. Parentheses ()
  2. Postfix increment ++ and decrement --
  3. Prefix increment ++ and decrement --, Unary plus + and minus -, Logical NOT !, Bitwise NOT ~, Size of sizeof
  4. Multiplication *, Division /, Modulo %
  5. Addition +, Subtraction -
  6. Bitwise shift << and >>
  7. Relational operators <, <=, >, and >=
  8. Equality operators == and !=
  9. Bitwise AND &
  10. Bitwise XOR ^
  11. Bitwise OR |
  12. Logical AND &&
  13. Logical OR ||
  14. Ternary conditional ?:
  15. Assignment =, Compound assignment +=, -= and so on.

Note that the order of evaluation can be changed by using parentheses to group the operations in the order we want them to be evaluated. Also, if two operators have the same precedence, the order of evaluation is determined by the associativity of the operators. For example, the addition and subtraction operators have the same precedence, and they are left associative, which means that the expression a + b - c is evaluated as (a + b) - c.