# Operator Precedence And Associativity In C Language With Examples

In this series of learning C language, today we are going to talk about Operator Precedence and Associativity In C Language.

Today we will learn in detail about what is Operator Precedence and Associativity in C language and why is it used.

So let’s know all things one by one. But if you have not read the article in Operators In C Language, then you will definitely read it.

Because Before knowing the Operator Precedence and Associativity in c language, you need to know what is operators and how many types of operators are there in C language, only then you will understand better.

Let’s now learn what is Operator Precedence.

## Operator Precedence In C

Operator Precedence is used when there is more than one operator, Operator precedence is a way to determine which operator will be first Execute and which Operator will be Execute later.

Now we learn why and how the concept of Operator Precedence came?

## Why this Precedence Concept Come into the Picture

Suppose we have a statement in which we have to do addition, multiplication, division, subtraction, etc in the same statement and if we do not have the concept of operator precedence, then we can solve it in either Left to Right and Right to Left orders. We will try, which will not be the right way because sometimes the result can come wrong, in such a situation, the developers introduced the concept of operator precedence.

Let’s understand the Operator Precedence well from the example given below:

int value = 10 + 20 * 10;

If the operator precedence was not conceptualized and we tried to solve left to right and right to left, then the result would have been something like this

- Calculate Left to Right then output: 300.
- Calculate Right to Left then output: 210.

When we do the same calculation with Operator Precedence,

- 20 * 10 = 200
- 10 + 200 = 210

In the C language, the precedence of the multiplication (*) operator is higher than the “+” operator, so first the operator with multiplication (*) will run, and then the + operator will run, which will result in 210 results.

By this time you must have understood which is the Operator Precedence. And why use Operator Precedence, now let’s know about Associativity.

## What is Associativity In C

When an Expression has two or more operators and their Precedence is also the same, then in that case we use Associativity to find out which operator will be Execute first and which operator will be Execute later.

For example – 10 * 2/5, in this example the multiplication (*) and division (/) operators have equal precedence, so in which case should we run the operator first so that the result is correct? Will the operator with first multiplication (*) be correct or will the operator with division (/) be correct?

To know all this, we also have to check the Associativity of those operators. Seeing the associativity of these operators, when we solve the Expression, the result is correct. The associativity of the operator in C language plays a very important role in such conditions.

In this Expression (10 * 2/5), the Associativity of Multiplication (*) and Division (/) operator is Left to Right, so we will first execute 10 * 2, then after that, we will divide the result from 5.

Let’s understand it with a program

```
#include <stdio.h>
int main ()
{
int x, y;
x = 10*20+110/5;
printf ("The Value of x = %d", x);
return 0;
}
```

Look at this example (10 * 20 + 110/5). In this the Precedence of multiplication (*) and division (/) operator is more than “+” operator,

So it is clear that the + operator will run after the * and / operator in the last.

But in this expression, first-run multiplication (*) operator or run division (/) operator? To know this, we have to read the association of these operators.

The Associativity of the stipulated multiplication (*) and division (/) operator is Left to Right, so the first * operator (10 * 20) will run, whose result will be 200.

After that, the “/” operator (110/5) will run, whose result will be 22 and then the “+” operator will run at the last which will result in the total result 222.

If you want to see its live demo, then click on the below live demo button and run this program and see its live demo.

The output of this program will be like this:

**Output -:**

`The Value of x = 222`

Below, I am giving you the list of Precedence and Associativity of all the operators. In this list, the operator whose precedence is the highest is placed on the top and the operator whose precedence is less is placed on the bottom. In this list, the associativity of those operators is also mentioned.

## Operator Precedence and Associativity Table in C Programming

Category | Operator | Associativity |

Postfix | () [] -> . ++ – – | Left to right |

Unary | + – ! ~ ++ – – (type)* & sizeof | Right to left |

Multiplicative | * / % | Left to right |

Additive | + – | Left to right |

Shift | << >> | Left to right |

Relational | < <= > >= | Left to right |

Equality | == != | Left to right |

Bitwise AND | & | Left to right |

Bitwise XOR | ^ | Left to right |

Bitwise OR | | | Left to right |

Logical AND | && | Left to right |

Logical OR | || | Left to right |

Conditional | ?: | Right to left |

Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |

Comma | , | Left to right |

**Read More -:**

- What is Operators & Types of Operators in C
- Arithmetic Operators In C
- Relational Operators In C
- Logical Operators In C
- Bitwise Operators In C
- Assignment Operators In C
- Conditional Operator in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download

## Conclusion

Friends, I hope you have found the answer of your question and you will not have to search about the Operator Precedence and Associativity in the C language

However, if you want any information related to this post or related to programming language, and computer science, then comment below I will clear your all doubts.

If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.

Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the Operator Precedence and Associativity in C language

To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.