What is operator precedence and associativity


Every C operator has a precedence (priority) associated with it. This precedence is used to determine how an expression involving more than one operator is evaluated. There are different levels of operator precedence and an operator may belong to one of these levels. The operators at the higher level of precedence are evaluated first. The operators in the same level of precedence are evaluated from left to right or from right to left, based on the associativity property of an operator. In the below table we can look at the precedence levels of operators and also the associativity of the operators within the same level. Rank 0 indicates the lowest precedence and Rank 14 indicates highest precedence.
The precedence and associativity of various operators in C are as given below:
An expression is a sequence of operands and operators that reduces to a single value. For example, the expression, 10+5 reduces to the value of 15. Based on the operators and operators used in the expression, they are divided into several types. Some of them are:
  1. Integer expressions – expressions which contains integers and operators
  2. Real expressions – expressions which contains floating point values and operators
  3. Arithmetic expressions – expressions which contain operands and arithmetic operators
  4. Mixed mode arithmetic expressions – expressions which contain both integer and real operands
  5. Relational expressions – expressions which contain relational operators and operands
  6. Logical expressions – expressions which contain logical operators and operands
  7. Assignment expressions and so on… – expressions which contain assignment operators and operands

Example 1

Let’s understand the operator precedence and associativity rules with the help of an example. The expression that we consider for this example is:
if(x==10+15&&y<10)
In the above expression, the operators used are: ==,+,&& and <. By looking at the operator precedence table, + operator is at level 4, < operator is at level 6, == operator is at level 7 and && operator is at level 11. So clearly, the + operation is performed first. Then our expression becomes:
if(x==25&&y<10)
Now, since < operator has the next highest precedence, y<10 is evaluated. If we assume value of x is 20 and value of y is 5, then the value of y<10 is true. Then the == operator is evaluated. Since value of x is 20, x==25 evaluates to false. So, our expression becomes:
if(false&&true)
Now, the only operator left is &&. It is evaluated next and the result is false.

Example 2

Let us try to evaluate an arithmetic expression as shown below:
x = a-b/3+c*2-1
Let a = 9, b =12, and c=3. Then our expression becomes:
x = 9-12/3+3*2-1
From the above table, we can see that the * and / operators are having higher precedence than + and – operators. Also, the * and / operators are at the same level of precedence, so we have to apply the associativity rules. Since the associativity rule is left-to-right, we apply the / operator first and the expression evaluates to:
x = 9-4+3*2-1
Next, we apply the * operator and the expression becomes:
x = 9-4+6-1
Next, we apply the first – operator as the – and + operators are at the same level and the associativity rule is from left to right. The expression becomes:
x = 5+6-1
Now, we apply the + operator and the expression become:
x = 11-1
Finally, we apply the – operator and the result is:
x = 10
Take your time to comment on this article.

Comments