**Article by:** Manish Methani

**Last Updated:** November 6, 2021 at 2:04pm IST

In C programming language, operators are used to perform various operations on data. Operators have different precedence and associativity which are essential for understanding the order in which expressions are evaluated. In this tutorial, we will discuss C Operator Precedence, Associativity of Operators: Definition and Examples with codes and examples.

Operator precedence determines the order in which operators are evaluated. It specifies which operator should be evaluated first and which one should be evaluated next. C Operator Precedence follows the rules of mathematics, where multiplication and division are evaluated before addition and subtraction.

The following table shows the precedence of operators in C programming language, from the highest precedence to the lowest:

Precedence | Operator | Description |
---|---|---|

1 | () | Parentheses |

2 | ++ -- | Increment and Decrement |

3 | * / % | Multiplication, Division, Modulus |

4 | + - | Addition and Subtraction |

5 | << >> | Bitwise Left Shift and Right Shift |

6 | < <= > >= | Relational Operators |

7 | == != | Equality Operators |

8 | & | Bitwise AND |

9 | ^ | Bitwise XOR |

10 | | | Bitwise OR |

11 | && | Logical AND |

12 | || | Logical OR |

13 | ?: | Conditional Operator |

14 | = += -= *= /= %= &= ^= |= <<= >>= | Assignment Operators |

The associativity of Operators determines the order in which operators of the same precedence are evaluated. C Operators can be grouped as left-associative or right-associative. The direction of associativity is from left to right or right to left.

Left associative operators are evaluated from left to right. For example, the addition and subtraction operators (+, -) are left-associative. In the following example, the expression (a + b - c) is evaluated from left to right:

int a = 5, b = 10, c = 2; int result = a + b - c; // Evaluates as (a + b) - c

Right associative operators are evaluated from right to left. For example, the assignment operator (=) is right-associative. In the following example, the expression (a = b = c) is evaluated from right to left:

int a, b, c; a = b = c = 10; // Evaluates as c = 10; b = c; a = b;

Let's see some examples to understand C Operator Precedence and Associativity.

int a = 5, b = 10, c = 2; int result = a * b + c; // Evaluates as (a * b) + c

In this example, the multiplication operator (*) has higher precedence than the addition operator (+), so the expression (a * b) is evaluated first and then added to c.

int a = 5, b = 10, c = 2; int result = a + b * c; // Evaluates as a + (b * c)

In this example, the multiplication operator (*) has higher precedence than the addition operator (+), so the expression (b * c) is evaluated first and then added to a.

In conclusion, understanding operator precedence and associativity is essential for writing efficient and correct C code. By following the rules defined in the operator precedence table, you can ensure that your expressions are evaluated in the correct order while understanding the associativity of operators helps you know whether they are evaluated from left-to-right or right-to-left. Armed with this knowledge, you can write C code that is easier to read, debug, and maintain. Remember to practice and experiment with examples to reinforce your understanding of operator precedence and associativity in C. With these concepts in your toolkit, you wll be well on your way to becoming a proficient C programmer.

10 OS Interview Questions you must know in 2024

10 Angular Interview Questions you must know in 2024

10 Javascript Interview Questions you must know in 2024

Top 10 DSA Interview Questions You should not miss

Top 10 Web Development Interview Questions

Introduction to Next.js

Altina Schinasi: Celebrating the Visionary Designer of Cat-Eye Frames | Google Doodle Tribute

Best Niche for Blogging in USA: A Comprehensive Guide

18 Must-Do Activities for Computer Science Students in College

13 Essential Things Solopreneurs need to know in 2024

25 Practical ways to earn money online

The Power of Gratitude - A Journey to Personal Growth

Top 5 Technology Trends That Are Shaping Our Future

5 Books You Need to Read Right Now

How to answer tell me about yourself?

What Angular feature transforms data before displaying it in the view?

Which widget is used in Flutter to create a button?

6 Mobile App Ideas That You Can Start in 2024

Flutter Widget Showdown: Exploring the Developers Favorite for Layouts!

Mobile App

For Engineers.

Mock Tests.

Test your C Programming skills with this comprehensive mock test on C Programming.

Take TestSolve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take TestSolve most asked GATE Questions in Operating Systems and test your Gate Score.

Take TestThis is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take TestSolve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test