# 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.

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.

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

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`