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:

  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.