Table of Contents
< All Topics
Print

10. Operator Precedence and Associativity

Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. As an example, the multiplication operator has a higher precedence than the addition operator. Certain operators have higher precedence than others.

Precedence of operators

The precedence of operators determines which operator is executed first if there is more than one operator in an expression.

Example,

x = 7 + 3 * 2;

Explanation:

Here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

The operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Associativity of Operators

·The associativity of operators determines the direction in which an expression is evaluated.

Example1:

b=a;

Explanation:

here the value of a is assigned to b, and not the other way around. It’s because the associativity of the = operator is from right to left.

·Also, if two operators of the same precedence (priority) are present, associativity determines the direction in which they execute.

Example:

1==2 != 33

Explanation

Here, operators == and != have the same precedence. And, their associativity is from left to right. Hence, 1 == 2 is executed first.

The expression above is equivalent to: (1==2) != 3.

Complete table of Precedence and Associativity

CategoryOperatorAssociativity
Postfix() [] -> . ++ – –Left to right
Unary+ – ! ~ ++ – – (type)* & sizeofRight to left
Multiplicative* / %Left to right
Additive+ –Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma,Left to right
Read twice the rows where associativity is from Right to Left.

When we move vertically Precedence matters and within a row associativity matter.

There are certain frequent dangers to be aware of while working with operator precedence and associativity in C. You can steer clear of mistakes and produce more dependable code by being aware of these risks and adhering to best practices.

1. Not using brackets when needed.

Use brackets to clearly indicate the order of evaluation when an expression contains multiple operators with different precedence. Failing to do so may result in inaccurate outcomes.

Example:

int result = 5+9*3-1;

Output

31

In this case, the division (/) operator has higher precedence than addition ( ) and subtraction (-). However, without parentheses, the expression is evaluated from left to right, resulting in 5+9*3-1= 31 which is incorrect.

To fix this, the expression should be written as:

int result = (5+9)*3 - 1;

Output

41

Now, the addition is evaluated first, followed by division and subtraction, resulting in the correct answer: (5+9) * 3 – 1 = 41.

Best Practice:

When there is ambiguity due to multiple operators, always use brackets to explicitly state the order of evaluation.

2. Neglecting associativity and relying only on operator precedence.

When operators have the same precedence, associativity takes over, but operator precedence controls the order of evaluation for operators with differing precedence. Unexpected outcomes can emerge from failing to take associativity into account.

Example:

int x = 5; 
int y = 7; 
int z = 3; 
int result = x - y - z;

In this case, both the subtraction operators have the same precedence. However, the associativity of subtraction is left-to-right. Therefore, the expression is evaluated as (x – y) – z, resulting in -5 – 3 = -8. If the desired result is 5 – (7 – 3) = 1, parentheses must be used: int result = x – (y – z);

Best Practice:

When creating expressions, especially when working with operators of the same precedence, take into account both operator precedence and associativity.

3. Misuse of similar symbols and different operators.

Stack in C offers Different operators, some of which may share symbols but have distinct semantics. Combining them may result in grammatical mistakes or unexpected behavior.

Example:

int x = 10; 
int y = 5; 
int result = x & y 1;  // Syntax error: mixing bitwise AND (&) with addition ( )

The ‘ & ‘ operator is an addition operator in this case, while the ‘&’ operator is a bitwise AND operator. A syntax error occurs when these operators are combined without the correct brackets. To overcome this, you can provide brackets to specify the evaluations’ intended sequence:

int result = (x & y) 1;

Best Practice: Be aware of the various operators that could share symbols and use brackets to clarify phrases as needed.

4. Misuse of the increment and decrement operators.

If not utilized properly, the increment ( ) and decrement (–) operators can lead to confusion. Unexpected outcomes can emerge from varying their placement within an expression or from utilizing them more than once within the same expression.

Example:

int x = 5; 
int y = 0; 
int result = x - --y;  // Undefined behavior: mixing and multiple usages of increment and decrement operators

In this example, the operators for postfix increment (++) and prefix decrement (–) are combined in the equation x – –y. This expression’s behavior is unclear because it is uncertain whether it will increase or decrease. It’s better to use these operators separately and explicitly to avoid such issues.

Best Practice:

Combining the increment and decrement operators in the same expression is not advisable. Ensure that they are used explicitly and in the intended evaluation sequence.

Points to Remember

Here are some important considerations for operator precedence and associativity in C:

  • 1.Operators are evaluated in order of higher precedence before lower precedence operators.
  • 2. To explicitly set the order of evaluation and override the default precedence, use brackets.
  • 3. To compare operators with the same precedence, associativity—which can be either left-to-right or right-to-left—is used.
  • 4. it is essential to comprehend operator precedence and associativity to create effective and proper C programs.

Conclusion

Associativity and operator precedence are fundamental concepts in the C programming language. Understanding these concepts and following the specified rules ensures that expressions are accurately evaluated and yield the desired results. Mastering operator precedence and associativity is essential for becoming a skilled C programmer.

Leave a comment