Skip to main content

Command Palette

Search for a command to run...

Who wins?

Updated
3 min read
J

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:

  1. 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 to a.

  2. 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 by c.

  3. 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:

PrecedenceOperatorDescriptionAssociativity
1() [] -> .Grouping, array subscript, structure and union member access, and member access through pointerLeft-to-right
2++ --Postfix increment and decrementLeft-to-right
+ -Unary plus and minusRight-to-left
! ~Logical NOT and bitwise NOTRight-to-left
(type)Type castRight-to-left
*Indirection (dereference)Right-to-left
&Address-ofRight-to-left
sizeofSize ofRight-to-left
3* / %Multiplication, division, and modulusLeft-to-right
4+ -Addition and subtractionLeft-to-right
5<< >>Bitwise left shift and right shiftLeft-to-right
6< <= > >=Relational operatorsLeft-to-right
7\== !=Equality operatorsLeft-to-right
8&Bitwise ANDLeft-to-right
9^Bitwise XORLeft-to-right
10Bitwise ORLeft-to-right
11&&Logical ANDLeft-to-right
12Logical ORLeft-to-right
13?:Conditional (ternary) operatorRight-to-left
14\= += -= *= /= %= <<= >>= &= ^=\=Assignment and compound assignment
15,CommaLeft-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.

More from this blog

Jyotiprakash's Blog

251 posts

I'm Jyotiprakash, a software dev and professor at KIIT, with expertise in system programming.