# Precedence and Associativity of Arithmetic Operators in C Language with Examples

## Introduction:

In our previous article, We have discussed about the **Arithmetic Operators in C Language.** In Today’s article, We are going to discuss about the ** Precedence and Associativity of Arithmetic operators** in C Programming Langauge.

## Precedence and Associativity of Arithmetic Operators

As we discussed earlier , We have five arithmetic operators in C Language.

- Addition ( + Operator )
- Subtraction ( - Operator )
- Multiplication ( * Operator )
- Division ( / Operator )
- Modulo Division ( % Operator)

Here is how we can evaluate the basic Arithmetic expressions.

## Example Expressions with Arithmetic Operators :

**
a = 5 + 6; ====> Result is 11 and 11 is assigned to variable
a (
a is Integer Variable Here).**

**
a = 10 - 5; ====> Result is 5 and 5 is assigned to variable
a.**

**
a = 5 * 10; ====> Result is 50 and 50 stored in
a now.**

It’s easy to evaluate the expression with single operator.

But what if the expression contains more than one operator like below.

**a = 5 – 3 + 2 ; **

Then how to evaluate the expression.

## How to Evaluate an Expression which have More than One Operator :

If an expression contains more than one operator, Then we will depend on the Precedence and Associativity of Operators.

Each C arithmetic operator have Precedence/Priority that means if we have more than one Operator in Expression. Then priority of operator will decides which operator should be evaluated first and which one is evaluated last.

The highest priority operator will be evaluated first, Then followed by the next priority Operator. and so on.

We have few Arithmetic Operators, Which have the same Precedence or Priority level. If your expression contains more than one operator from the same precedence level, Then we need to use the Associativity of arithmetic Operators.

Here is the table containing the Precedence and Associativity of Arithmetic Operators in C.

📢 As we are presently looking at arithmetic operators, Below table contains only arithmetic operators priority and associativity. To know the full list of Operators Precedence and Associativity – **Operators Precedence and Associativity in C Language – SillyCodes**

## Arithmetic Operators Precedence and Associativity table:

Precedence | Operator | Description | Associativity |
---|---|---|---|

1 |
* / % | Multiplication Division Modulo Division | Left to Right |

2 |
+ - | Addition Subtraction | Left to Right |

From above table we can see that Arithmetic operators have** two priority levels**.

** Multiplication( * ) , Division( / ) and Modulo Division( % )** Operators have

__same priority.__** Subtraction( – ) and Addition( + )** Operators have

**same Priority**.

Whenever we have more than one operator with same priority level, Then the **Associativity of Operators** will decides which operators to evaluate first.

**📢 All Arithmetic Operators have Associativity from Left to Right.**

## Quick Example on Associativity of Operators:

Let’s say we have an expression with Multiplication Operator ( *) and Division Operator ( /).

To evaluate this expression, First We need to check the Precedence of both operators, But both * Operator and / Operator have the Same Priority.

Now we need to check the Associativity of product and division operators. The Associativity of all arithmetic operators is from Left to Right. So above expression with * Operator and / Operator will be evaluated from Left to Right.

Let’s take an example and apply above logic.

**Example Expression:**

**
a = 5 * 10 / 5 ;**

** step 1 :** Checking

**, Here Two Operators have same priority.**

__Priorities of Operators__** step 2 : ** Associativity of Operators, The Arithmetic Operators have

**Associativity.**

__Left to Right__So first **
5*10** will calculated result is
50.

Next 50/5 will calculated the Result is 10. and 10 will be assigned to variable a

So Final Result of above 5 * 10 / 5expression is 10.

By using the above method, We can calculate any kind of Expression in Simple way by following priority and Associativity of Operators.

## Few More Examples on Arithmetic Operators :

Here are few more complex arithmetic operators examples, Let’s try to evaluate them using the above Precedence and Associativity rules.

**Example 1:**

a = 8 + 2 - 5 ;

Result will be a = 5;

**Example 2:**

a = 100 * 200 / 100 * 100 + 100;

Result is a = 20100;

**Example 3:**

a = 20 + 30 * 10 - 50 / 10 % 2;

Result is a = 319;

As this third example is complex one, Here is the explanation of it.

**Explanation:**

We have all arithmetic operators in the above expression. So We need to follow the Precedence of Operators to evaluate the expression.

The high precedence Operators are
* ,
/ and
% operators. All three Operators have the Same Precedence level. So we need to depend on the Associativity. The Associativity of Multiplication, Division and Modulo division Operators is from **Left to Right**. So we need to evaluate these three operators from left to right.

Once we evaluated the high precedence operators, Then we need to evaluate the low precedence operators like addition + and Subtraction - Operators.

Here is the step by step Explanation of above expression evaluation.

__Step 1 :__

The 30 * 10 calculated result is 300.

After first calculation Expression will became as below

a = 20 + 300 - 50 / 10 % 2 ;

__Step 2 :__

We will evaluate 50 / 10 = 5;

After the **Step 2**, The expression is will be

a = 20 + 300 - 5 % 2;

__Step 3 :__

In the next step, We will evaluate 5 % 2 = 1. Because modulo division gives Remainder. So after this step, The expression will become

a = 20 + 300 - 1;

__Step 4 : __

Now we have two low priority operators and these Operators associativity also **Left to Right**.

So first Addition ( +) Operation is Performed then subtraction ( -) is performed.

Expression after Step 3 : a = 20 + 300 - 1;

Perform Addition,

20 + 300 = 320; Then the Expression became

a = 320 - 1;

__step 5 :__

Now Perform the Subtraction Operation.

320 - 1 = 319 ;

The final value of expression is 319

Assign the it to variable a

a = 319;

That’s it. We got the desired result. So by following these simple priority and Associativity Rules, We can Calculate Very Complex Expressions.

## Related Articles:

**Start Here – Step by step tutorials to Learn C programming Online with Example Programs – SillyCodes****Arithmetic Operators with Examples.****Compilation Stages in C Programming.****Structure of C Program.****Character set of C language.****Identifiers and Rules for Naming Identifiers.****Features and Characteristics of C Language.**