C Operator Precedence, Associativity of Operators: Definition and Examples

3 min 30 sec read Basic


Associativity is used when two operators of same precedence appear in an expression. Associativity of operators in C can be either Left to Right or Right to Left. For example ‘*’ and ‘/’ have same precedence and their associativity is Left to Right, so the expression “100 / 10 * 10” is treated as “(100 / 10) * 10”.

Why Associativity is used ?

Example :-

100 / 10 + 10 

Here / has higher precedence or priority than + So compiler here knows how to solve this equation. It will be solved as (100/10) + 10 = 20

Consider this example :-

100 / 10 *10 

Here / and * have same precedence that means both of these operators have same priorities. So how does compiler now know how to solve this ?. Even though order of precedence is important associativity of operators has to be taken into consideration when two operators has same precedence like in this case. Here associativity defines the order to solve this equation from left to right(100/10) * 10 or from right to left 100/ (10*10). And since / * operators have associativity from left to right.

So the equation

100 / 10 *10 
will be solved like this 
(100/10) * 10 = 10

Example:

100 / (10 *10)
will be solved like this 
100 / 100 

Output:

1

Since ( ) brackets have higher precedence or priority than any other operators. Always remember this.

Following table list the C operators according to their order of precedence and associativity :-

Operators Description Associativity
( ) Parentheses Left - Right
[ ] Array Subscripts
. Member selection via object name
-> Member selection via pointer
++ — Postfix increment/decrement
 
++ — Prefix increment/decrement right-to-left
+ – Unary plus/minus
! ~ Logical negation/bitwise complement
(type) Cast (convert value to temporary value of type)
* Dereference
& Address (of operand)
sizeof Determine size in bytes on this implementation
 
* / % Multiplication/division/modulus left-to-right
+ – Addition/subtraction
<< >> Bitwise shift left, Bitwise shift right
< <= Relational less than/less than or equal to
> >= Relational greater than/greater than or equal to
== != Relational is equal to/is not equal to
& ^ Bitwise AND, Bitwise OR
| Bitwise inclusive OR
&& Logical AND
| | Logical OR
 
? : Ternary conditional right-to-left
= Assignment
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment
%= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<= >>= Bitwise shift left/right assignment
<<= >>= Bitwise shift left/right assignment
 
, Comma (separate expressions) left-to-right

 

So this was all about C Operator Precedence and Associativity of Operators in C language. In next article we will cover different types of operators in C. Happy Learning !

 

Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials