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.