Master C Operator Precedence and Associativity in 2023: Definition, Examples, and Code

Article by: Manish Methani

Last Updated: November 6, 2021 at 2:04pm IST
3 mins 46 sec read

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.

C Operator Precedence

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

Associativity of 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

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

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;

Examples

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

Example 1:

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.

Example 2:

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.

Discover My FlutterFlow Courses and Template Apps

FlutterFlow Course: Basic to Advanced (2025) – Dating App
Learn to Create the Dating App with Admin Dashboard using No-Code Tool FlutterFlow in this comprehensive course in 2025.
FlutterFlow Course: Learn to Build the Grocery Delivery App
Learn to Create Grocery-Delivery App in FlutterFlow, ideal for anyone looking to implement a functional E-Commerce App.
FlutterFlow Course: Learn to Build the Online Courses App
Learn to Create Online Courses App in FlutterFlow, ideal for anyone looking to implement a functional Courses App.
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com