# C Operators Precedence

There are so many operators in C language, how they affect the output of an expression when multiple operators are used together is decided by the operator precedence.

Based on the operator precedence, the C compiler decides the order in which the operators are evaluated.

For example, if we have three variables `a`, `b` and `c`, then for the expression `a+b*c`, the compiler will first multiply `b` and `c`, and then add the result of multiplication with `a`, because the `*` operator has higher precedence than the `+` operator.

Not all operators can be used together in a single expression, hence the operator precedence is only relevant for the operators that can be used together, and they are:

1. Arithmetic operators

2. Relational operators

3. Logical operators

## Order of Precedence:

The arithmetic operators hold higher precedence than the logical and relational operators.

For example, if we have the following expression,

``10 > 1 + 9;``

This will return false because first the arithmetic operator `+` will be evaluated and then the comparison will be done. The above expression is treated as `10 > (1+9)`.

Hence, because 10 is not greater than 10, but it is equal, so the expression will return false.

## Order of Precedence in Arithmetic Operators

There are many arithmetic operators in the C language, and the order of precedence is applicable for them too.

1. `++` and `--` (increment and decrement) operators hold the highest precedence.

2. Then comes `-` (unary minus) operator

3. Then comes `*`, `/` and `%` holding equal precedence.

4. And at last, we have the `+` and `-` operators used for addition and subtraction, with the lowest precedence.

Operators on the same level of precedence are evaluated by the compiler from left to right.

We can use parentheses to change the order of the evaluation.

Let's see an example,

``````#include <stdio.h>

int main() {
// arithmetic operator precedence
int a = 10, b = 20, c = 30, result;

result = a * b + ++c;

printf("The result is: %d", result);

return 0;
}``````

The result is: 231

In the above code, first, `++c` is evaluated because the increment operator has the highest precedence (value becomes 31), then `a*b` is evaluated because next in order of precedence is the multiplication operator (value becomes 200), then the `+` operator is evaluated (200 + 31), hence the result is 231.

## Order of precedence in Relational/Logical Operators

In the case of the relational and logical operators, this order is followed:

1. The `!` (logical NOT) operator holds the highest precedence.

2. Then comes `>` (greater than), `>=` (greater than or equal to), `<` (less than), and `<=` (less than or equal to) with the same precedence.

3. Then we have the `==` and `!=` operators.

4. After that comes the `&&` (logical AND) operator

5. At last, we have the `||` (logical OR) operator with the lowest precedence.

For example, the expression `!0 && 0 || 0`, will give false as result.

Because first the NOT operator gets evaluated, which gives us 1, then `1 && 0` will give us 0, and then finally `0 || 0` will also give us 0, which is false.

## Using Parentheses

We can control the order of execution by using parentheses in our expressions. Taking the last example, if we write the expression like this `!(0 && 0) || 0`, then the result will become true. Because then first the expression `0 && 0`, which is inside the parentheses gets evaluated, then the NOT operator, and then the OR operator.

Similarly, we can change the order of the arithmetic operators too, using the parentheses.

## Conclusion:

In this tutorial, we learned the order of precedence of the different C language operators along with learning how using parentheses can change that.