Who wins?
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class.
At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out.
In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.
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 * cis 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 / bis 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.