Home Contact About

Precedence of arithmetic operators

In C programming, arithmetic operators have a specific precedence that determines the order in which they are evaluated. This means
certain operators are evaluated before others, regardless of the order in which they appear in the expression. The operator precedence
rules in C are similar to those in standard algebra, and they help ensure that expressions are evaluated correctly and produce the expected results.

The following table shows the operator precedence for arithmetic operators in C, arranged from highest to lowest:

  1. Parentheses ( )
  2. Unary operators: +, -, ++, --, !
  3. Multiplicative operators: *, /, %
  4. Additive operators: +, -
  5. Relational operators: <, >, <=, >=
  6. Equality operators: ==, !=
  7. Logical AND operator: &&
  8. Logical OR operator: ||
  9. Conditional operator: ? :
  10. Assignment operators: =, +=, -=, *=, /=, %=

Operators with higher precedence are evaluated before operators with lower precedence. For example, in the expression a + b * c, the
multiplication operator has higher precedence than the addition operator, so b * c is evaluated first.

If two operators have the same precedence, the order of evaluation is determined by associativity. For example, the additive operators
have the same precedence, and they are evaluated from left to right. This means that in the expression a + b - c, the addition is
evaluated first, followed by the subtraction.

It's important to note that the precedence and associativity rules in C can be overridden by using parentheses to explicitly specify the
order of evaluation. For example, in the expression (a + b) * c, the addition is evaluated first because it is inside parentheses.

By understanding the operator precedence rules in C, you can create complex arithmetic expressions that produce the expected results.
It's important to use parentheses when necessary to ensure that expressions are evaluated in the correct order.