# 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.

1. Addition ( + Operator )
2. Subtraction ( - Operator )
3. Multiplication ( * Operator )
4. Division ( / Operator )
5. 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:

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 Priorities of Operators , Here Two Operators have same priority.

step 2 : Associativity of Operators, The Arithmetic Operators have Left to Right Associativity.

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;

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 C Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

### 1 Response

1. [â€¦] Precedence and Associativity of Arithmetic Operators [â€¦]