C Tutorial C Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C - Operators Precedence



C Operators Precedence

Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.

Operator Associativity

Operator associativity is the direction in which an expression is evaluated. For example:

int x = 10;
int y = 20;

//associativity of = operator is
//Left to Right, hence x will become 20
x = y;

As the associativity of = is left to right. Hence x is assigned the value of y.

Example:

The example below illustrates the operator precedence in C.

#include <stdio.h>
 
int main (){
  int retval1, retval2, retval3;

  //evaluates 5 * 2 first
  retval1 = 15 - 5 * 2;

  //above expression is equivalent to
  retval2 = 15 - (5 * 2);

  //forcing compiler to evaluate 15 - 5 first
  retval3 = (15 - 5) * 2;

  printf("15 - 5 * 2 = %i\n", retval1);
  printf("15 - (5 * 2) = %i\n", retval2);
  printf("(15 - 5) * 2 = %i\n", retval3);
  return 0;
}

The output of the above code will be:

15 - 5 * 2 = 5
15 - (5 * 2) = 5
(15 - 5) * 2 = 20

C Operators Precedence Table

The following table lists the precedence and associativity of C operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.

PrecedenceOperatorDescriptionAssociativity
1a++  a--Postfix increment, Postfix decrementLeft to Right
a()Function call
a[]Array subscripting
.Structure and union member access
->Structure and union member access through pointer
(type){list}Compound literal
2++a  --aPrefix increment, Prefix decrementRight to Left
+a  -aUnary plus, Unary minus
!Logical NOT
~Bitwise NOT
(type)Cast
*aIndirection (dereference)
&aAddress-of
sizeofsizeof operator
_AlignofAlignment requirement
3*  /  %Multiplication, Division, RemainderLeft to Right
4+  -Addition, Subtraction
5<<  >>Bitwise left shift and right shift
6<  <=  >  >=Less than, Less than or equal, Greater than, and Greater than or equal
7==  !=Equality and Inequality
8&Bitwise AND
9^Bitwise XOR
10|Bitwise OR
11&&Logical AND
12||Logical OR
13a?b:cConditional (ternary) operatorRight to Left
14=Direct sssignment
+=  -=  *=  /=  %=Compound assignment by sum, difference, product, quotient and remainder
<<=  >>=Compound assignment by Bitwise left shift and right shift
&=  ^=  |=Compound assignment by Bitwise AND, XOR and OR
15,CommaLeft to Right

❮ C - Operators

5