C Programs Tutorials | IT Developer
IT Developer

C Programming - C Operator Precedence



Share with a Friend

C Programming - C Operator Precedence

C Operator Precedence

Operator precedence in C determines the order in which operators are evaluated in expressions. Operators with higher precedence are evaluated before those with lower precedence. In cases where operators have the same precedence, associativity decides the evaluation order (whether they are evaluated from left to right or right to left).

Operator Precedence in C

The following table summarizes the precedence of operators in C, listed from the highest to the lowest precedence:

Precedence Level Operator(s) Description

1

()

Parentheses (used for grouping expressions)

2

[] -> .

Array subscripting, member selection, pointer dereference

3

++ -- (prefix)

Increment and Decrement (prefix form)

4

! ~ + - (unary)

Logical NOT, Bitwise NOT, Unary plus, Unary minus

5

* / %

Multiplication, Division, Modulus

6

+ -

Addition, Subtraction

7

<< >>

Bitwise left shift, Bitwise right shift

8

< <= > >=

Relational operators

9

== !=

Equality and Inequality operators

10

&

Bitwise AND

11

^

Bitwise XOR

12

`

`

13

&&

Logical AND

14

`

 

15

? :

Ternary conditional operator

16

= += -= *= /= %= &= ^= `

= <<= >>=`

17

,

Comma operator

Associativity of Operators

  • Left-to-right associativity: Most operators in C (such as arithmetic, relational, logical, and bitwise operators) follow left-to-right evaluation.
    • Example: a + b - c is evaluated as ((a + b) - c).
  • Right-to-left associativity: Assignment operators and the ternary conditional operator (? :) follow right-to-left evaluation.
    • Example: a = b = c is evaluated as a = (b = c).

Operator Precedence Rules

  1. Parentheses ():
    • Anything inside parentheses is evaluated first, overriding all operator precedences.
    • Example: (a + b) * c evaluates a + b first, then multiplies the result by c.
  2. Unary Operators (++, --, +, -, !, ~):
    • These operators have higher precedence than most other operators, so they are evaluated first.
    • Example: ++a + b increments a before adding b.
  3. Multiplication, Division, Modulus (*, /, %):
    • These operators have higher precedence than addition and subtraction, so they are evaluated first when mixed.
    • Example: a + b * c evaluates as a + (b * c).
  4. Relational Operators (<, <=, >, >=):
    • These operators have lower precedence than arithmetic and bitwise operators but are evaluated before logical operators.
    • Example: a + b > c evaluates a + b first, then compares the result to c.
  5. Logical Operators (&&, ||):
    • These operators have lower precedence than relational and arithmetic operators but higher precedence than assignment operators.
    • Example: a > b && c > d evaluates both conditions before the logical AND operation.
  6. Assignment Operators (=, +=, -=):
    • Assignment operators have the lowest precedence (except for the comma operator).
    • Example: a = b + c first evaluates b + c, then assigns the result to a.

Parentheses to Control Precedence

You can always use parentheses () to control the order of evaluation explicitly in expressions, even when the operator precedence gives a different order.

Example Without Parentheses:

C

int result = 5 + 2 * 3;

Here, * has higher precedence than +, so the result is:

C

5 + (2 * 3) = 5 + 6 = 11

Example With Parentheses:

C

int result = (5 + 2) * 3;

Here, parentheses override the precedence, so the result is:

C

(5 + 2) * 3 = 7 * 3 = 21

Operator Precedence in Action

C

#include <stdio.h>

int main() {

    int a = 5, b = 10, c = 20, result;

    // Applying precedence rules

    result = a + b * c;  // result = 5 + (10 * 20) = 5 + 200 = 205

    printf("Result of a + b * c: %d\n", result);

    result = (a + b) * c;  // result = (5 + 10) * 20 = 15 * 20 = 300

    printf("Result of (a + b) * c: %d\n", result);

    result = a + b > c;  // result = (5 + 10) > 20 = 15 > 20 = 0 (false)

    printf("Result of a + b > c: %d\n", result);

    return 0;

}

Output:

Result of a + b * c: 205

Result of (a + b) * c: 300

Result of a + b > c: 0

Summary

  • The precedence of operators determines the order in which operations are performed in C expressions.
  • Parentheses can be used to override default precedence to ensure the desired order of evaluation.
  • Operators with higher precedence are evaluated first, and associativity defines how operators with the same precedence are evaluated.