Precedence and Associativity of Arithmetic Operators in C Language with Examples

Spread the love

Precedence-and-Associativity-of-Operators-in-C

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:

PrecedenceOperatorDescriptionAssociativity
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 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;

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:

Related C Programs:

Venkat

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

You may also like...

Leave a Reply