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:

  1. Postfix Operators (Highest Precedence):
    • '()' Function call
    • []' Array subscripting
    • ->' Member selection through pointer
    • .' Member selection
    • ++' Post-increment
    • '--' Post-decrement
  2. 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
  3. Multiplicative Operators:
    • '*' Multiplication
    • '/' Division
    • '%' Modulus
  4. Additive Operators:
    • '+' Addition
    • '-' Subtraction
  5. Shift Operators:
    • '<<' Left shift
    • '>>' Right shift
  6. Relational Operators:
    • '<' Less than
    • '>' Greater than
    • '<=' Less than or equal to
    • '>=' Greater than or equal to
  7. Equality Operators:
    • '==' Equal to
    • '!=' Not equal to
  8. Bitwise AND Operator:
    • '&' Bitwise AND
  9. Bitwise XOR Operator:
    • '^' Bitwise XOR
  10. Bitwise OR Operator:
    • '|' Bitwise OR
  11. Logical AND Operator:
    • '&&' Logical AND
  12. Logical OR Operator:
    • '||' Logical OR
  13. Conditional (Ternary) Operator:
    • '? :' Conditional operator
  14. 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:

  1. '()' Parentheses: Used to control the order of evaluation. (a + b) is evaluated first.
  2. '+' Addition: Adds a and b after evaluating (a + b).
  3. '*' Multiplication: Multiplies the result of (a + b) by c.
  4. '%' Modulus: Calculates the remainder when a is divided by b.
  5. '/' Division: Divides the result of (a % b) by 2.
  6. '-' Subtraction: Subtracts the result of (a % b) / 2 from the product of (a + b) * c.
  7. '+' Addition: Adds 1 to the previous result.

Here's how the evaluation proceeds:

  1. '(a + b)' is evaluated first: 5 + 3 results in 8.
  2. '(a % b)' is calculated: 5 % 3 results in 2.
  3. '(a % b) / 2' is computed: 2 / 2 results in 1.
  4. '(a + b) * c' is calculated: 8 * 2 results in 16.
  5. The subtraction step: 16 - 1 results in 15.
  6. Finally, '1' is added: 15 + 1 results in 16.

So, the final result of the expression is 16.