×

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

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

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

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.