# Hierarchy of Operations in C language

While executing an arithmetic statement, which has two or more operators, we may have some problems with how it is executed exactly. For example, does the expression 2 * x – 3 * y correspond to (2x) – (3y) or 2 (x-3y)? Similarly, A / B * C correspond to A / (B * C) or (A / B) * C?

To answer these questions satisfactorily, we must understand the “hierarchy” of operations. The priority or priority in which the operations in an arithmetic statement are performed is called the hierarchy of operations. The hierarchy of commonly used operators is shown in Figure.

Now some tips on using operators in general.

• In parentheses, the same hierarchy mentioned in figure 1.11 is operational. Also, if there is more than one set of parentheses, the operations in the most inner brackets would be performed first, followed by the operations in the second inner pair and so on.
• We must always remember to use pairs of parentheses. A careless imbalance of the right and left brackets is a common mistake. The best way to avoid this error is to type () and then type an expression inside it.

Some examples would further clarify the problem.

Example 1.1: Determine the hierarchy of operations and evaluate the following expression:

`i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8 `

The step by step evaluation of this expression is presented below:

```i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8
i = 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8   operation: *
i = 1 + 4 / 4 + 8 - 2 + 5 / 8       operation: /
i = 1 + 1+ 8 - 2 + 5 / 8            operation: /
i = 1 + 1 + 8 - 2 + 0               operation: /
i = 2 + 8 - 2 + 0                   operation: +
i = 10 - 2 + 0                      operation: +
i = 8 + 0                           operation: -
i = 8                               operation: + ```

Note that 6/4 gives 1 and not 1.5. This is because 6 and 4 are both integers and therefore would only evaluate an integer constant. Similarly, 5/8 is evaluated to zero, because 5 and 8 are constant integers and therefore must return a full value.

Example 1.2: Determine the hierarchy of operations and evaluate the following expression:

The step by step evaluation of this expression is presented below:

`kk = 3 / 2 * 4 + 3 / 8 + 3 `

```kk = 3 / 2 * 4 + 3 / 8 + 3

kk = 1 * 4 + 3 / 8 + 3          operation: /
kk = 4 + 3 / 8 + 3              operation: *
kk = 4 + 0 + 3                  operation: /
kk = 4 + 3                      operation: +
kk = 7                          operation: + ```

Note that 3/8 gives zero, again for the same reason mentioned in the previous example.

All operators in C are classified according to their precedence. And remember that there are C up to 45 odd operators in C and these it can affect the evaluation of an expression in subtle and unexpected ways if we are not careful. Unfortunately, there are no simple rules to follow, such as “BODMAS” to tell students in algebra that the order evaluates an expression. We haven’t met many of these 45 operators, so we will not pursue the topic of priority here. However, at this stage it can be realized that it would be almost impossible to remember the precedence of all these operators. This may seem daunting, but when its content is absorbed into small bites, it becomes more enjoyable.

So far we have seen how the computer calculates an arithmetic statement written in C. But our knowledge would be incomplete unless we know how to convert a general arithmetic statement into an instruction C. C language can handle any complex expression easily.

Some of the examples of C expressions are shown in Figure.