Skip to main content

Operator Precedence and Associativity

Operator Precedence

  • Operator precedence determines which operation is performed first in an expression.

  • It refers to the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence.

For example:

  • In the expression a + b * c, the multiplication operator (*) has higher precedence than the addition operator (+), so b * c is evaluated first, and then the result is added to a.

Operator Associativity

  • Operators Associativity is used when two operators of same precedence appear in an expression.

  • Determines the order in which operations with the same precedence are evaluated. There are two types of associativity: left-to-right and right-to-left.

Operators with left-to-right associativity are evaluated from left to right.

For example:

  • In the expression a - b - c, the subtraction operator (-) has left-to-right associativity, so the expression is evaluated as (a - b) - c.

Operators with right-to-left associativity are evaluated from right to left.

For example:

  • In the expression a = b = c, the assignment operator (=) has right-to-left associativity, so the expression is evaluated as a = (b = c).

Operator Precedence and Associativity Table

Following table shows the Operator Precedence and Associatvity in C:

OperatorOperator NamePrecedenceAssociativityDescription
()Function call1Left-to-rightFunction call
[]Array subscript2Left-to-rightArray subscript
.Structure member access3Left-to-rightStructure member access
->Structure pointer member access4Left-to-rightStructure pointer member access
++postfix increment5Left-to-rightIncrements the value of the operand by 1 after the expression is evaluated
--postfix decrement5Left-to-rightDecrements the value of the operand by 1 after the expression is evaluated
++prefix increment6Right-to-leftIncrements the value of the operand by 1 before the expression is evaluated
--prefix decrement6Right-to-leftDecrements the value of the operand by 1 before the expression is evaluated
+unary plus7Right-to-leftUnary plus operator
-unary minus7Right-to-leftUnary minus operator
!logical NOT8Right-to-leftLogical NOT operator
~bitwise NOT8Right-to-leftBitwise NOT operator
*dereference9Right-to-leftDereference operator
&address-of9Right-to-leftAddress-of operator
sizeofsizeof9Right-to-leftSize
*multiplication10Left-to-rightMultiplication operator
/division10Left-to-rightDivision operator
%modulo10Left-to-rightModulo operator
+addition11Left-to-rightAddition operator
-subtraction11Left-to-rightSubtraction operator
<<bitwise left shift12Left-to-rightBitwise left shift operator
>>bitwise right shift12Left-to-rightBitwise right shift operator
<less than13Left-to-rightLess than operator
<=less than or equal to13Left-to-rightLess than or equal to operator
>greater than13Left-to-rightGreater than operator
>=greater than or equal to13Left-to-rightGreater than or equal to operator
==equal to14Left-to-rightEqual to operator
!=not equal to14Left-to-rightNot equal to operator
&bitwise AND15Left-to-rightBitwise AND operator
^bitwise XOR16Left-to-rightBitwise XOR operator
""bitwise OR17Left-to-right
&&logical AND18Left-to-rightLogical AND operator
""logical OR19
?:ternary operator20Right-to-leftternary operator
"="assignment21Right-to-leftassignment
+=addition assignment21Right-to-leftTernary operator (conditional operator)
-=subtraction assignment21Right-to-leftAssignment operator
*=multiplication assignment21Right-to-leftAddition assignment operator
/=division assignment21Right-to-leftSubtraction assignment operator
%=modulo assignment21Right-to-leftMultiplication assignment operator
&=bitwise AND assignment21Right-to-leftDivision assignment operator
^=bitwise XOR assignment21Right-to-leftModulo assignment operator
=bitwise OR assignment21Right-to-left
<<=bitwise left shift assignment21Right-to-leftBitwise left shift assignment operator
>>=bitwise right shift assignment21Right-to-leftBitwise right shift assignment operator
,comma operator22Left-to-rightComma operator

Operator Precedence Example

Below program shows the operator precedence in C:

#include <stdio.h>

int main() {
int x = 10;
int y = 20;
int z = 30;

printf("x + y * z = %d\n", x + y * z); // x + y * z = 210
printf("x * y + z = %d\n", x * y + z); // x * y + z = 650
printf("x / y + z = %d\n", x / y + z); // x / y + z = 30
printf("x / (y + z) = %d\n", x / (y + z)); // x / (y + z) = 0
printf("x / y * z = %d\n", x / y * z); // x / y * z = 0
printf("(x + y) * z = %d\n", (x + y) * z); // (x + y) * z = 900
return 0;
}
Output:

Explanation

  • In the first line of the program, the multiplication is done first, and then the addition.
  • In the second line of the program, the multiplication is done first, and then the addition.
  • In the third line of the program, the division is done first, and then the addition.
  • In the fourth line of the program, the parentheses are used to change the order of evaluation, so the addition is done first, and then the division.
  • In the fifth line of the program, the division is done first, and then the multiplication
  • In the sixth line, the addition is done first, and then the multiplication.

Operator Associativity Example

Below program shows the Operator Associativity in C:

#include <stdio.h>

int main() {
int x = 10;
int y = 20;
int z = 30;

printf("x - y - z = %d\n", x - y - z); // x - y - z = -40
printf("x / y / z = %d\n", x / y / z); // x / y / z = 0
printf("x - (y - z) = %d\n", x - (y - z)); // x - (y - z) = 20
printf("(x / y) / z = %d\n", (x / y) / z); // (x / y) / z = 0
printf("x / (y / z) = %d\n", x / (y / z)); // x / (y / z) = 2
return 0;
}
Output:

Explanation

  • In the first line of the program, the subtraction is done from left to right, so the value of x is subtracted from the value of y, and then the value of z is subtracted from the result.
  • In the second line of the program, the division is done from left to right, so the value of x is divided by the value of y, and then the result is divided by the value of z.
  • In the third line of the program, the parentheses are used to change the order of evaluation, so the subtraction is done first, and then the value of x is subtracted from the result.
  • In the fourth line of the program, the division inside the parenthesis is done first, and then the division outside the parenthesis is done.
  • In the fifth line, the division inside the parenthesis is done first, and then the division outside the parenthesis is done.

Best Practices

Lastly, Below are some of the best practices when dealing with Operator Precedence and Associativity in your programming:

  • Use parentheses: Always use parentheses to indicate the intended order of evaluation, making your code easier to read and understand.

  • Default operator precedence and associativity: Always be aware of the default operator precedence and associativity of C programming language, and use them consistently throughout your code.

  • Whitespaces around operators: Use whitespaces around operators to make the code more readable and easy to understand.

  • Multiple operators with the same precedence: Avoid using multiple operators with the same precedence in the same line of code, as it can be confusing.

  • Thoroughly test your code: Thoroughly test your code with a variety of inputs and edge cases to ensure that it is robust and reliable.

  • Document complex expressions: Always document any non-standard or complex expressions in your code to help other developers understand the intended behavior. When in doubt, use parentheses to make the order of operations explicit.

  • Follow the conventions: Follow the conventions of the language, if any, to make the code easy to understand by other developers who might read it in the future.