C - Operators Precedence and Associativity
In C and many other programming languages, operators have precedence and associativity rules that determine the order in which operators are evaluated in expressions. Understanding these rules is essential for writing correct and predictable code. Operators are grouped into categories based on their precedence, and within each category, they may have left-to-right or right-to-left associativity. Here's an overview:
Precedence:
Operators with higher precedence are evaluated before operators with lower precedence. If an expression contains multiple operators, those with higher precedence are evaluated first. If operators have the same precedence, their order of evaluation depends on associativity.
Associativity:
Associativity determines the order of evaluation when operators of the same precedence appear in an expression. Operators can be left-associative (evaluated from left to right) or right-associative (evaluated from right to left).
Here's a summary of common C operators, their precedence, and associativity:
-
Postfix Operators (Highest Precedence):
-
'()' Function call
- []' Array subscripting
- ->' Member selection through pointer
- .' Member selection
- ++' Post-increment
- '--' Post-decrement
-
Unary Operators:
-
'+' Unary plus
- '-' Unary minus
- '!' Logical NOT
- '~' Bitwise NOT
- '&' Address-of
- '*' Dereference (indirection)
- 'sizeof' Size of object
- 'sizeof' Size of data type
- '_Alignof' Alignment requirement
-
Multiplicative Operators:
-
'*' Multiplication
- '/' Division
- '%' Modulus
-
Additive Operators:
-
'+' Addition
- '-' Subtraction
-
Shift Operators:
-
'<<' Left shift
- '>>' Right shift
-
Relational Operators:
-
'<' Less than
- '>' Greater than
- '<=' Less than or equal to
- '>=' Greater than or equal to
-
Equality Operators:
-
'==' Equal to
- '!=' Not equal to
-
Bitwise AND Operator:
-
Bitwise XOR Operator:
-
Bitwise OR Operator:
-
Logical AND Operator:
-
Logical OR Operator:
-
Conditional (Ternary) Operator:
-
'? :' Conditional operator
-
Assignment Operators (Lowest Precedence):
-
'=' Assignment
- '+=' Addition assignment
- '-=' Subtraction assignment
- '*=' Multiplication assignment
- '/=' Division assignment
- '%=' Modulus assignment
- '<<=' Left shift assignment
- '>>=' Right shift assignment
- '&=' Bitwise AND assignment
- '^=' Bitwise XOR assignment
- '|=' Bitwise OR assignment
Keep in mind that parentheses ( )
can be used to override the default precedence and associativity of operators and control the order of evaluation.
Here's an example illustrating operator precedence and associativity:
#include <stdio.h>
int main() {
int a = 5, b = 3, c = 2;
int result = a + b * c; // Multiplication (*) has higher precedence than addition (+)
printf("Result: %d\n", result); // Output: Result: 11
return 0;
}
In this example, b * c
is evaluated first because multiplication has higher precedence than addition, resulting in 6
. Then, a + 6
is evaluated, giving a final result of 11
.
For more understanding let's create an example with multiple expressions that cover a variety of operators and their precedence:
#include <stdio.h>
int main() {
int a = 5, b = 3, c = 2;
int result = (a + b) * c - (a % b) / 2 + 1;
printf("Result: %d\n", result);
return 0;
}
In this example, we have the following operators and expressions:
-
'()' Parentheses: Used to control the order of evaluation.
(a + b)
is evaluated first.
-
'+' Addition: Adds a and b after evaluating
(a + b)
.
-
'*' Multiplication: Multiplies the result of
(a + b)
by c
.
-
'%' Modulus: Calculates the remainder when
a
is divided by b
.
-
'/' Division: Divides the result of
(a % b)
by 2
.
-
'-' Subtraction: Subtracts the result of
(a % b) / 2
from the product of (a + b) * c
.
-
'+' Addition: Adds
1
to the previous result.
Here's how the evaluation proceeds:
-
'(a + b)' is evaluated first:
5 + 3
results in 8
.
-
'(a % b)' is calculated:
5 % 3
results in 2
.
-
'(a % b) / 2' is computed:
2 / 2
results in 1
.
-
'(a + b) * c' is calculated:
8 * 2
results in 16
.
-
The subtraction step:
16 - 1
results in 15
.
-
Finally, '1' is added:
15 + 1
results in 16
.
So, the final result of the expression is 16
.