×

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.

operatorleftrightdescription
/53 or 3*2The right operator is unclear while the left operator is not
*5/3 or 32Unlike 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.

operatorleftrightdescription
=xy or y = 4The left operand is clear-cut, but the right is not
=y or x = y4The 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 operatorDescription of operatorAssociativity of operator
()Call for a functionLeft to right
[]Array element citationsLeft to right
->Indirect selection of membersLeft to right
.Direct selection of membersLeft to right
!logical oppositionRight to left
~complement of bitwise(1's)Right to left
+Unary plusRight to left
-Unary minusRight to left
++Increment operatorRight to left
--Decrement operatorRight to left
&Address or dereferenceRight to left
*Pointer referenceRight to left
sizeofreturns an object's sizeRight to left
typeTypecast (conversion)Right to left
*MultiplicationLeft to right
/divisionLeft to right
%RemainderLeft to right
+Binary plus or additionLeft to right
-Binary minus or subtractionLeft to right
<< Left shiftLeft to right
>> Right shiftLeft to right
< Less thanLeft to right
<=Less than or equal toLeft to right
> Greater thanLeft to right
>=Greater than or equal toLeft to right
==Equal toLeft to right
!=Not equal toLeft to right
&Bitwise ANDLeft to right
|Bitwise ORLeft to right
^Bitwise exclusive ORLeft to right
&&Logical ANDLeft to right
||Logical ORLeft to right
?:Conditional operatorRight to left
=Assignment operatorRight to left
*=Assign productRight to left
/=Assign quotientRight to left
%=Assign remainderRight to left
+=Assign sumRight to left
-=Assign differenceRight to left
&= Assign Bitwise ANDRight to left
^= Assign Bitwise XORRight to left
|=Assign Bitwise ORRight to left
<<=Assign left shiftRight to left
>>=Assign right shiftRight to left
,Separator of expressionsLeft 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

Related Topics

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.