Who wins?
Operator precedence and associativity are important concepts in C that determine the order in which operators are evaluated in an expression. Precedence defines the priority of operators, while associativity defines the order in which operators of the same precedence are evaluated.
Let's create a code sample that demonstrates operator precedence and associativity:
#include <stdio.h>
int main() {
int a = 5, b = 3, c = 2, result;
// Example 1: Demonstrating Operator Precedence
result = a + b * c;
printf("Example 1: a + b * c = %d\n", result);
// Example 2: Demonstrating Associativity
result = a / b * c;
printf("Example 2: a / b * c = %d\n", result);
// Example 3: Parentheses to Override Precedence
result = (a + b) * c;
printf("Example 3: (a + b) * c = %d\n", result);
return 0;
}
Explanation:
Example 1: Demonstrates operator precedence. In C, multiplication (
*
) has higher precedence than addition (+
). So,b * c
is evaluated first, and then the result is added toa
.Example 2: Demonstrates associativity. In this case, both division (
/
) and multiplication (*
) have the same precedence. In C, the associativity of these operators is left-to-right. So,a / b
is evaluated first, and then the result is multiplied byc
.Example 3: Shows the use of parentheses to override precedence. The expression inside the parentheses is evaluated first, and then the result is multiplied by
c
.
Now, let's see the output:
Example 1: a + b * c = 11
Example 2: a / b * c = 3
Example 3: (a + b) * c = 16
This output illustrates the impact of operator precedence and associativity on the evaluation order of expressions in C.
Here is a table listing the precedence and associativity of various operators in C. The operators are arranged in decreasing order of precedence:
Precedence | Operator | Description | Associativity | ||
1 | () [] -> . | Grouping, array subscript, structure and union member access, and member access through pointer | Left-to-right | ||
2 | ++ -- | Postfix increment and decrement | Left-to-right | ||
+ - | Unary plus and minus | Right-to-left | |||
! ~ | Logical NOT and bitwise NOT | Right-to-left | |||
(type) | Type cast | Right-to-left | |||
* | Indirection (dereference) | Right-to-left | |||
& | Address-of | Right-to-left | |||
sizeof | Size of | Right-to-left | |||
3 | * / % | Multiplication, division, and modulus | Left-to-right | ||
4 | + - | Addition and subtraction | Left-to-right | ||
5 | << >> | Bitwise left shift and right shift | Left-to-right | ||
6 | < <= > >= | Relational operators | Left-to-right | ||
7 | \== != | Equality operators | Left-to-right | ||
8 | & | Bitwise AND | Left-to-right | ||
9 | ^ | Bitwise XOR | Left-to-right | ||
10 | Bitwise OR | Left-to-right | |||
11 | && | Logical AND | Left-to-right | ||
12 | Logical OR | Left-to-right | |||
13 | ?: | Conditional (ternary) operator | Right-to-left | ||
14 | \= += -= *= /= %= <<= >>= &= ^= | \= | Assignment and compound assignment | ||
15 | , | Comma | Left-to-right |
It's important to note that operators with higher precedence are evaluated before those with lower precedence. Operators with the same precedence are evaluated based on their associativity. The associativity column indicates the order of evaluation when multiple operators of the same precedence appear in an expression.