Associativity of Operators in C
What is an Associativity operator?
Two operators with equal priority are connected by employing their association when they are present in an expression.
There are two different types of associativity:
- left to right
- right to left.
The left operand must be clear when using the left-associative right. Without any room for doubt? It shouldn't be considered when other subexpressions are being evaluated. Similar to the previous one, the right operand in the case of association must be clear.
Let’s see some examples to understand this clearly
Example 1:
Consider an expression given below
a = 5/3*2;
Between the operators of the same priority, / and *, there is a relationship in this instance. The associative * and / are used to settle this relationship. Both, however, like the linkage of Left to Right. The figure identifies which of each operator's operations is clear-cut and which is not for each operator.
operator | left | right | description |
/ | 5 | 3 or 3*2 | The right operator is unclear while the left operator is not |
* | 5/3 or 3 | 2 | Unlike the left operator, the right operator is clear. |
As only / has the left operand unambiguously (a need for associating L with R), whereas both/and * have associative L with R, it is executed earlier.
Example 2:
Consider an expression given below
x = y = 4;
In this case, both assignment operators have the same association and priority (from right to left). The figure demonstrates which operand for each operator is clear-cut and which is not.
operator | left | right | description |
= | x | y or y = 4 | The left operand is clear-cut, but the right is not |
= | y or x = y | 4 | The right operand is clear-cut, whereas the left is not |
Both = have R to L associativity, but only the second = operates as unequivocal (a need for R to L associativity), hence the second = is completed first.
Why the associativity operator is used?
Associativity is used when there are two operators in an expression having the same precedence. Associativity is worthless when the order of the operators is different.
It is crucial to realize that an operator's associativity does not determine the evaluation order of its operands.
Let's imagine that we wish to compute 12/3*2 in the C programming language. We can observe that the division and multiplication operators have the same precedence from the precedence and associativity table that is provided below. Why then does the compiler not become confused about which calculation to perform first? Multiplication or division?
The associativity of operators is used to avoid this misunderstanding.Two operators' associativity comes into play when they share the same precedence. Due to the associativity of the division and multiplication operators (left to right), the operator written on the left is evaluated first. As a result, in the example above, division comes before multiplication.
Precedence
For the associativity of operators, there will be a concept of precedence which has to be followed. whereas the associativity of operators is defined below
C has a predefined rule of operator priority if an expression contains multiple operators. Operator precedence is the term used to describe this operator priority rule.
In C, arithmetic operators (*,%,/,+,-) are prioritized above relational operators (==,!=,>,>=,=), while relational operators are prioritized over logical operators (&&, ||, and!).
Table of Operator’s Associativity
Type of operator | Description of operator | Associativity of operator |
() | Call for a function | Left to right |
[] | Array element citations | Left to right |
-> | Indirect selection of members | Left to right |
. | Direct selection of members | Left to right |
! | logical opposition | Right to left |
~ | complement of bitwise(1's) | Right to left |
+ | Unary plus | Right to left |
- | Unary minus | Right to left |
++ | Increment operator | Right to left |
-- | Decrement operator | Right to left |
& | Address or dereference | Right to left |
* | Pointer reference | Right to left |
sizeof | returns an object's size | Right to left |
type | Typecast (conversion) | Right to left |
* | Multiplication | Left to right |
/ | division | Left to right |
% | Remainder | Left to right |
+ | Binary plus or addition | Left to right |
- | Binary minus or subtraction | Left to right |
<< | Left shift | Left to right |
>> | Right shift | Left to right |
< | Less than | Left to right |
<= | Less than or equal to | Left to right |
> | Greater than | Left to right |
>= | Greater than or equal to | Left to right |
== | Equal to | Left to right |
!= | Not equal to | Left to right |
& | Bitwise AND | Left to right |
| | Bitwise OR | Left to right |
^ | Bitwise exclusive OR | Left to right |
&& | Logical AND | Left to right |
|| | Logical OR | Left to right |
?: | Conditional operator | Right to left |
= | Assignment operator | Right to left |
*= | Assign product | Right to left |
/= | Assign quotient | Right to left |
%= | Assign remainder | Right to left |
+= | Assign sum | Right to left |
-= | Assign difference | Right to left |
&= | Assign Bitwise AND | Right to left |
^= | Assign Bitwise XOR | Right to left |
|= | Assign Bitwise OR | Right to left |
<<= | Assign left shift | Right to left |
>>= | Assign right shift | Right to left |
, | Separator of expressions | Left to right |
Example programs
Example 1
#include <stdio.h>
int main(){
int a = 5/3*2;
printf("a=%d\n", a);
return 0;
}
Output
a=2
Example 2
Program for associativity of a prefix
#include <stdio.h>
void main()
{
int arr[] = {1, 2, 3};
int *p = arr;
++*p;
printf("arr[0] = %d, arr[1] = %d, *p = %d",
arr[0], arr[1], *p);
}
Output
arr[0] = 2, arr[1] = 2, *p = 2
Example 3
Program for associativity of a postfix
#include <stdio.h>
void main()
{
int arr[] = {1, 2, 3};
int *p = arr;
*p++;
printf("arr[0] = %d, arr[1] = %d, *p = %d",
arr[0], arr[1], *p);
}
Output
arr[0] = 1, arr[1] = 2, *p = 2
Example 4
#include <stdio.h>
void main()
{
int a = 1, x;
x = a++ + ++a;
printf("%d", x);
}
Output
4