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.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

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

Take Test

Flutter Test

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

Take Test

GATE(CSE) Operating Systems

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

Take Test

HTML,CSS Test

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

Take Test

(GATE CSE) Data Structures & Algorithms Test

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

Take Test
include_once 'codzify-footer.php'; ?>