What is the Associativity of Operators in C?
The associativity of operators in C determines the order of evaluation of operators in an expression containing multiple operators and/or brackets if the precedence of the operators is the same.
The precedence of an operator determines which operator is evaluated first within an expression. The precedence essentially expands on the concept of the BODMAS rule in mathematics and applies it to all operators used within a programming language, in this case, C. All operators are given a specified precedence with certain operators having given the same precedence. In case we are to evaluate an expression having two or more operators with the same precedence, It becomes important to know which of the operators is to be evaluated first. This is where the associativity of operators in C comes into play.
The associativity of operators determines in which direction an expression is evaluated within a program, i.e. either left to right or right to left. This helps us determine which operator to evaluate if two or more operators of the same precedence appear in a program. The associativity essentially tells the compiler the direction from which it should start evaluating the expression. Thus if an expression has two or more operators with the same precedence and the associativity of the operators is from left to right, then the compiler will evaluate the leftmost operator first and move to the right.
For example, if we are to evaluate an expression 12/4*6, both / and * have the same precedence in C. However, the associativity of / and * is left to right and hence 12/4 is evaluated first giving 3 which is then multiplied with 6 to give 18. If the associativity of the operators were right to left instead, the results would be drastically different.
It is important to note that the associativity of operators in C with the same precedence is always the same. Otherwise, the compiler would not be able to evaluate the expression. It is also important to note that the associativity of operators does not determine the order of evaluation of the operands. That is, if the operands require evaluation on their own, their evaluation is not dependent on the associativity of the operator. Say you have an expression like functionA()+functionB(). Even if the associativity of the + operator is left to right, whether functionA is to be evaluated first or functionB is determined by the compiler.
Operator Associativity Table in C
The following table lists the associativity of operators in C. The table is listed in decreasing order of precedence. As mentioned before, operators with the same preference have the same associativity. The associativity column provides the direction of evaluation of expression containing the operators.
Type | Operators | Associativity |
---|---|---|
Postfix | () [] -> . ++ - - | Left to right |
Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left |
Multiplicative | * / % | Left to right |
Additive | +`` - | Left to right |
Shift | << >> | Left to right |
Relational | < <=`` > >= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %= >>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |
Examples of Associativity of Operators in C
Let us take a look at a few examples to understand the associativity of operators in C
Example 1:
Output:
The precedence of *,/, and % is the same. Yet, as the associativity of multiplicative operators is from left to right ans hence the calculations go as follows. We have added brackets for easy visualization:
Example 2:
Output:
The precedence of both the equality operators is the same. Yet, as the associativity of the equality operators is from left to right, the calculations go as follows. We have added brackets for easy visualization:
Example 3:
Output:
The precedence of the assignment operators += and -= is the same. Yet, as the associativity of the assignment operators is from right to left, the calculations go as follows. We have added brackets for easy visualization:
Example 4:
Output:
In the expression above, there are a number of operators with varying precedences. Within the expression, the brackets are evaluated first. Within the bracketed expression, the operator precedence of left to right for multiplicative operators is followed. Operator precedence is also followed to evaluate the remaining operators outside the brackets. The right-to-left precedence of assignment operators is followed and hence b is evaluated first followed by a. The calculation goes as follows. We have added brackets for easy visualization:
Learn more About Operator Precedence and Associativity of Operators in C
To learn more about operator precedence and associativity of operators in C, refer to the following article in Scaler Topics.
Conclusion
- Associativity of operators in C determines the order of execution of operators having the same preference.
- Associativity of operators in C is either left to right or right to left.
- Operators with the same precedence have the same associativity.