In this article, you'll learn about the precedence and associativity of operators when executing an expression.

If more than one operators are involved in an expression, C language has a predefined rule of priority for the operators. This rule of priority of operators is called operator precedence.

In C, precedence of arithmetic operators( *, %, /, +, -) is higher than relational operators(==, !=, >, <, >=, <=) and precedence of relational operator is higher than logical operators(&&, || and !).

(1 > 2 + 3 && 4) This expression is equivalent to: ((1 > (2 + 3)) && 4) i.e, (2 + 3) executes first resulting into 5 then, first part of the expression (1 > 5) executes resulting into 0 (false) then, (0 && 4) executes resulting into 0 (false)

**Output**

0

If two operators of same precedence (priority) is present in an expression, Associativity of operators indicate the order in which they execute.

1 == 2 != 3

Here, operators `==`

and `!=`

have same precedence. The associativity of both `==`

and `!=`

is left to right, i.e, the expression on the left is executed first and moves towards the right.

Thus, the expression above is equivalent to :

((1 == 2) != 3) i.e, (1 == 2) executes first resulting into 0 (false) then, (0 != 3) executes resulting into 1 (true)

**Output**

1

The table below shows all the operators in C with precedence and associativity.

**Note:** Precedence of operators decreases from top to bottom in the given table.

Operator | Meaning of operator | Associativity |
---|---|---|

() [] -> . |
Functional call Array element reference Indirect member selection Direct member selection |
Left to right |

! ~ + - ++ -- & * sizeof (type) |
Logical negation Bitwise(1 's) complement Unary plus Unary minus Increment Decrement Dereference Operator(Address) Pointer reference Returns the size of an object Type cast(conversion) |
Right to left |

* / % |
Multiply Divide Remainder |
Left to right |

+ - |
Binary plus(Addition) Binary minus(subtraction) |
Left to right |

<< >> |
Left shift Right shift |
Left to right |

< <= > >= |
Less than Less than or equal Greater than Greater than or equal |
Left to right |

== != |
Equal to Not equal to |
Left to right |

& | Bitwise AND | Left to right |

^ | Bitwise exclusive OR | Left to right |

| | Bitwise OR | Left to right |

&& | Logical AND | Left to right |

|| | Logical OR | Left to right |

?: | Conditional Operator | Left to right |

= *= /= %= -= &= ^= |= <<= >>= |
Simple assignment Assign product Assign quotient Assign remainder Assign sum Assign difference Assign bitwise AND Assign bitwise XOR Assign bitwise OR Assign left shift Assign right shift |
Right to left |

, | Separator of expressions | Left to right |