Precedence and Associativity of Operators in C

How to use the Precedence and Associativity of the operators smartly is one of the important part of C programming.

Precedence talks about the priority among the different operators, which to consider first. Like arithmetic operators have higher priority than assignment operators and so on. When we have more than one operators in a single statement then this precedence comes into the picture as the result may vary greatly.
For eg – 2 + 3 * 4 – 1 == 2+ (3 * 4) – 1 = 16  => This happens because precedence of multiplication operator is higher than the other two.

Associativity comes into picture when we have operators of the same precedence. It doesn’t talk about which to pick first rather says the order of evaluation.

See the table,

Precedence and Associativity Table
Precedence and Associativity Table

How to use the table

Down the rows in the table shows the priorities decreasing.
Under each row, all the operators have the same priority, there comes the associativity.
It can be clearly seen, Unary, Ternary and Assignment Operators are evaluated right to left.

1) Logical Operator

“x == 5  &&  x == 10 || x != 0 “. In this statement relational operator(== and !=) is executed first then, logical AND (&&) and at last logical (OR).
“ X == 5 || X == 10 || X == 20”As relational operator has Left to Right associativity, therefore. The statement will be executed as (take note of the added brackets)

As relational operator has Left to Right associativity, therefore. The statement will be executed as (take note of the added brackets)
“ (X == 5 || X == 10) || X == 20”.

2) Assignment Operator

#include <stdio.h>

int main(void){		

    int x = 5, y = 5;
    x == y ? x = 10 : (y = 10); 

	/* x == y ? x = 10 : y = 10;
    * if we write above expression without parenthesis, there will be compiler error.
    * As y = 10 is the assignment operator that goes right to left.
    * 10 is assigned to y. Without parenthesis it says 10 is assigned to "x==y?x=10:".
    * Hence the compilation error will be Lvalue required	  
    */			

    printf("x = %d y = %d", x, y);
    return 0;
}
Output:-
x = 10 y = 5

3) Comma operator

Comma(,) is worked as both operator and separator and has least priority.

Comma as an operator acts like a sequence point. When evaluating an expression with more than one operand separated by a comma, it’ll evaluate and discard all except the last one.

#include <stdio.h>

int main(void){		
  
  int a = 1, b = 2, c = 3, x;
  x = (a, b, c); //discards a and b, assign c to x
  printf("x = %d", x);

}
Output:-
x = 3

Comma behaves as a separator in the case of function calls and definitions, variable declarations, enum declarations, and similar constructs.

#include <stdio.h>

int main(void){		
  
	int a = 1, b = 2, c = 3, x;
	x = a, b, c;
	/*act as separator a is assigned to x,
	*assignment operator has higher priority than comma operator*/
	
	printf("x = %d", x);

}
Output:-
x = 1

Post-increment and decrement operator

The post increment/decrement operator behaves in a unique way. It has the highest priority(hence, applied to the immediate variable), however, it’s done i.e. reflected in the memory after the statement is completed.
See the example below,

#include <stdio.h>

int main(void){
	int x = 10, y =5;
	
	y = x++ + y;    	
	printf("x = %d y = %d", x, y);
	
	/* post incr ++ is has highest priority, so x becomes 11 but
	 * it'll increase only after the statement is evaluated, so it is not reflectred in the value of 'y'
	 * y = 10 + 5
	 * x = x + 1 
	 * */
	return 0;
}
Output:-
x = 11 y = 15

An example involving pointer:

#include<stdio.h>
int main(void){
			
	int x[] = {1, 2, 3, 4, 5};
	int *p;
	int i;	
	p = x;  // p pointing to same first index of array
	
	printf("Before Pointer operation:");
	for(i = 0; i < 5; i++)
		printf("%d ", *(p+i));
			
	printf("\nAfter post increment based Pointer operation:");
	printf("\n%d\n", *p++);   
	/*only pointer will be incremented not the value and value printed will be of *p 
	 * without incremented address
	 * (*p) and then p++
	 * */	
	
	for(i = 0; i < 4; i++)
		printf("%d ", *(p+i));
		
		
	printf("\nAfter pre increment based Pointer operation:");
	printf("\n%d\n", *++p);
	/* ++ and * both unary operator, same precedence. Associavity is right to left
	 * content of p is read then increment done and then dereferenced i.e. access the value
	 *++p and then *p
	 * */
	 		
	for(i = 0; i < 3; i++)
		printf("%d ", *(p+i));	
			
	return 0;
	}
Output:-
Before Pointer operation:
1 2 3 4 5 
After post increment based Pointer operation:
1
2 3 4 5 
After pre increment based Pointer operation:
3
3 4 5 
After pre increment based Pointer operation:
4
4 4 5 

Knowledge is most useful when liberated and shared. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.
Keep Learning… Happy Learning.. 🙂

Recommended -