Operator Precedence and Associativity in C
The principles in C that govern operator evaluation order inside an expression are operator precedence and associativity. These regulations guarantee that statements are consistently analyzed and give the desired outcomes.
Operator Precedence
The precedence of operators in an expression determines their order of importance. Operators having greater precedence are assessed before those with a lower one. The associativity criteria apply when there is a tie in precedence between two operators. The following operators in C are listed from highest to lowest precedence:
Category | Operator | Associativity |
Postfix | () [] -> . ++ - - | Left to right |
Unary | + - ! ~ ++ - - (type)* & sizeof | Right 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 |
Program
#include <stdio.h>
int main () {
int a = 10;
int b = 5;
int c = 3;
int result1 = a + b * c;
int result2 = (a + b) * c;
printf ("Result 1: %d\n", result1);
printf ("Result 2: %d\n", result2);
return 0;
}
Output
![Operator Precedence and Associativity in C](https://static.tutorialandexample.com/c/operator-precedence-and-associativity-in-c1.png)
Few things to keep in mind while using Operator Precedence
- Know the order of precedence for various operators in C so that you can understand the precedence of operators. Operators are evaluated in order of greater precedence before lower precedence operators.
- When it's required, use parenthesis to group expressions and make the evaluation clear. For example, if you wish to impose an evaluation order that differs from the normal precedence.
- Unary operators are given higher priority: Most unary operators have higher precedence than most binary operators, including increment/decrement (++/--), negate (-), logical not (!), and others.
- The operations of addition and subtraction come after multiplication and division: The operations of addition (+) and subtraction (-) come after multiplication and division (*). Make careful to keep this in mind while creating expressions using these operators.
Operator Associativity
In the absence of parenthesis, associativity defines how operators with the same precedence are grouped. Both left-to-right associativity and right-to-left associativity are different forms of associativity.
1. Left associativity
When numerous operators with the same precedence appear sequentially, they are evaluated from left to right because of left associativity. The majority of C operators adhere to left associativity. For instance:
int result = a - b - c;
2. Right associativity
When numerous operators with the same precedence appear sequentially, they are evaluated from right to left due to a property known as right associativity. In C, there are just two operators with the right associativity:
- Assignment operator
int a, b, c;
a = b = c;
The assignment operator (=) in this phrase is right-associative. B is given the value of c, and an is subsequently given the value of b.
- Ternary operator
int max = (a > b)? a: b;
The ternary operator (?:) in this phrase is right-associative. The value of an is selected if the condition a > b is true; otherwise, the value of b is selected.
Program
#include <stdio.h>
int main () {
int a = 10;
int b = 5;
int c = 3;
int result1 = a - b - c;
int result2 = a = b = c;
printf ("Result 1: %d\n", result1);
printf ("Result 2: %d\n", result2);
printf ("a: %d, b: %d, c: %d\n", a, b, c);
return 0;
}
Output
![Operator Precedence and Associativity in C](https://static.tutorialandexample.com/c/operator-precedence-and-associativity-in-c2.png)
Few things to keep in mind while using Operator Associativity
- Associativity to the left is the default: Most C's operators are left-associative, including the arithmetic, bitwise, and logical operators. It indicates that operators are evaluated from left to right when they appear sequentially and have the same priority.
- Assignment and ternary operators have right-associativity: The assignment operator (=) and the ternary operator (?:) have this property. Evaluation goes right to left when numerous assignment or nested ternary operators exist.
- When necessary, enclose phrases in parentheses: Use parentheses to combine expressions and expressly declare the intended associativity if you wish to override the default associativity and impose a particular evaluation order.
- Be mindful of the effect on expression evaluation: Determining the sequence of operations in complicated expressions requires a thorough understanding of associativity. To prevent unexpected outcomes, ensure you are aware of the associativity of operators.