50% users failed to get the best score. It's your turn to test now.

Questions curated by the expert mentors at codzify.com

__Article__

3 min 30 sec read

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

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

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

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

1

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

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 !

1) Write C programs to implement i) strcpy(), ii) strlen() library function by yourself

2) Write C program to insert a node after a given node in a linked list

3) How to use %5.2f format specifier in C Programming?

4) Question on C Program referencing the Increment and Decrement Operators concept asked in GATE CSE

5) Article on different storage classes in C.

6) C Tutorial on Calloc() vs malloc() functions is explained in depth in this article.

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.

(C, C++, Python)

Execute your HTML, CSS and javascript code in real time with the web editor

(HTML, CSS, Bootstrap, Javascript)