Precedence & 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 operator 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 the picture when we have operators of the same precedence. It doesn’t talk about which to pick first rather says the order of evaluation.

Here is the precedence and associativity in C table,

Precedence and Associativity Table
Precedence and Associativity Table

1. How to use the Precedence and Associativity table in C?

Down the rows in the table shows the priorities decreasing.
Under each row, all the operators have the same priority, there comes associativity.

It can be clearly seen, Unary, Ternary, and Assignment Operators are evaluated right to left.

1.1. Logical Operator

“x == 5  &&  x == 10 || x != 0 “

In the above statement relational operator(== and !=) is executed first then, logical AND (&&), and at last logical (OR).

“ X == 5 || X == 10 || X == 20”

A relational operator has Left to Right associativity, therefore. The statement will be executed as (take note of the added brackets)

A 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”.

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

1.3. Comma operator

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

Comma as an operator acts as 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

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

Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ash
ash
2 years ago

associativity of unary operator is wrong
it should be !,~,++,–,+-,*,sizeof

Last edited 2 years ago by ash
1
0
Would love your thoughts, please comment.x
()
x
Index