C Programming

What is C Operator Precedence and Associativity

Operators are commonly used to perform various arithmetic and logical operations in C. The C programming language follows a specific order known as operator precedence and associativity to determine the order of operations during execution. The rules of operator precedence and associativity help in writing error-free code and increase code readability.

This article provides a comprehensive tutorial to assist you in learning about associativity and operator precedence in C.

Operator Precedence in C

Operator precedence describes the sequence in which several operators are evaluated while evaluating expressions. For instance, if an expression has both addition and multiplication operators, which one should be evaluated first? The answer is simple, C follows certain rules to resolve such ambiguity, commonly known as operator precedence. In C, operator precedence is classified into 17 levels, ranging from unary operators to the ternary conditional operator. Most used operators like arithmetic operators, relational operators, logical operators, and bitwise operators follow the C standard operator precedence protocol.

Precedence rules dictate which operators have higher priority than others when evaluating expressions. Several types of operators in the C programming language have varying degrees of precedence. For instance, unary operators have a greater priority than binary operators. The following is a list of operators sorted in ascending order of precedence:

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 (Address)

Pointer reference

Returns the size of an object

Typecast (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 Right to left
=
*=
/=
%=
+=
-=
&=
^=
|=
<<=
>>=
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

These rules guide the compiler on how to evaluate expressions with multiple operators in the same expression. The multiplication operator, for instance, has greater precedence than the addition operator in the equation A + B * C, in accordance with the precedence rules. Therefore, the compiler will first evaluate the expression B * C before adding A to the result.

Let’s look at an example of operator precedence through code.

#include <stdio.h>

int main() {

  int a = 43, b = 11, c = 16, d = 31;

  int result = ++a * b-- + c / --d;

printf("a = %d\n", a);

printf("b = %d\n", b);

printf("c = %d\n", c);

printf("d = %d\n", d);

printf("result = %d\n", result);

  return 0;

}

The four variables a, b, c, and d, are declared in the above code, and their initial values are 43, 11, 16, and 31 accordingly. Then, it applies a variety of arithmetic and assignment operators to these variables in an expression. The expression increases the value of a by using the pre-increment operator ++a, multiplies the outcome by the post-decrement operator b–, and then adds the outcome to the dividing c by the pre-decremented value of d. (using the pre-decrement operator –d). The variable result is then used to hold the total outcome of this expression.

Output

Associativity in C

Associativity refers to the order in which the operators are evaluated during an expression with the same precedence. Left to right and right to left associativity are the two forms of associativity in C. If two operators have the same precedence, they are assessed from left to right according to the concept of left-to-right associativity. Nevertheless, if the same precedence is observed, the operators are evaluated from right to left according to right-to-left associativity.

For example, the ++ increment operator has right-to-left associativity, which means the increment operator is evaluated after the variable. Whereas the logical AND operator has a left-to-right associativity, which means the operator is evaluated from left to right.

#include <stdio.h>

  int main() {

  int a = 6, b = 3, c = 2;

int result = a * b / c;

printf("result = %d\n", result);

  return 0;

}

The three variables a, b, and c are declared in this code, and they are initialized to 6, 3, and 2 respectively. Then, it applies multiplication and division operations to these variables in an expression. Multiplication and division are ranked from left to right in C based on their associativity as they have the same precedence level. This indicates that the division operation comes first, followed by the multiplication operation when evaluating the expression, a * b / c.

Output

Conclusion

Operator precedence and associativity are crucial concepts in C programming that help determine the order of execution of expressions. Operators with the highest precedence level are evaluated first and associativity rules help determine the order when multiple operators with the same precedence are used. Understanding operator precedence and associativity is essential when designing complex programs, and it helps produce cleaner and more efficient code with fewer errors.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.