?>
profile image
Announcements
No new announcements.
mentora icon
bookmark
Table of Contents

    Operator Precedence in C Programming

    In this lesson, we will learn what is the rule of Operator Precedence in C programming and how it works with some examples.

    What is Operator Precedence

    Operator Precedence in C programming is a rule that describe which operator is solved first in an expression. For example: * and / have same precedence and their associativity is Left to Right, so the expression 18 / 2 * 5 is treated as (18 / 2) * 5.

    Video Explanation

    Click the button below to watch the explanation.

    video image

    Let's take another example say x = 8 + 4 * 2; here value of x will be 16 and not 24 why, because * operator has a higher precedence than + operator. So 4*2 gets multiplied first and then adds into 8.

    Operator Associativity means how operators of the same precedence are evaluated in an expression. Associativity can be either from left to right or right to left.

    Basic Operator Precedence

    When solving arithmetic equations, follow this order:

    1. Brackets ( )

    If brackets are there in an equation then it will be solved first.

    Example:
      (2 + 3) * 4 -> first do 2 + 3 -> then multiply.

    2. Multiplication, Division, and Modulus: *, /, %

    These are equal in priority. Solve from left to right as they appear in an equation.

    Example:
       12 / 2 * 3
       -> First do 12 / 2 = 6
       -> Then 6 * 3 = 18

    Do not do multiplication before division or vice versa — just go left to right.

    3. Addition and Subtraction: +, −

    These also have equal priority. Solve from left to right as they appear in an equation.

    Example:
       10 − 3 + 2
       -> First do 10 − 3 = 7
       -> Then 7 + 2 = 9

    Now let's see some examples for more understanding.

    Example
    int x = 45 % 2 + 3 * 2;   // result of x is 7
    Explanation
    45 % 2 + 3 * 2
    45 % 2 + 3 * 2   will be solved as per operator precedence rule
    1 + 3 * 2    will be solved as per operator precedence rule
    1 + 6    will be solved as per operator precedence rule
    7 (Answer)
    Example
    int x = 19 + (5 / 2) + 4 % 2 - 6 * 3;   // result of x is 3
    Explanation
    19 + (5 / 2) + 4 % 2 - 6 * 3
    19 + (5 / 2) + 4 % 2 - 6 * 3     will be solved as per operator precedence rule
    19 + 2 + 4 % 2 - 6 * 3     will be solved as per operator precedence rule
    19 + 2 + 0 - 6 * 3     will be solved as per operator precedence rule
    19 + 2 + 0 - 18     will be solved as per operator precedence rule
    21 + 0 - 18     will be solved as per operator precedence rule
    21 - 18     will be solved as per operator precedence rule
    3 (Answer)

    List of Operator Precedence in C

    The table below shows all the operators in C with precedence and associativity.

    OPERATOR DESCRIPTION ASSOCIATIVITY
    ( )
    [ ]
    . ->
    ++ --
    Parentheses
    Array Subscript
    Member Selector
    Post-Increment / Decrement
    left to right
    ++ --
    + -
    ! ~
    (type)
    *
    &
    sizeof
    Pre-Increment / Decrement
    Unary plus / minus
    not operator and bitwise complement
    type casting
    Dereference operator
    Address of operator
    Determine size in bytes
    right to left
    * / % Multiplication, Division and Modulus left to right
    + - Addition and Subtraction left to right
    <<
    >>
    Bitwise left shift
    Bitwise right shift
    Left to Right
    < <=
    > >=
    relational less than / less than or equal to
    relational greater than / greater than or equal to
    left to right
    == != Relational equal to / not equal to left to right
    & Bitwise AND left to right
    ^ Bitwise exclusive OR left to right
    | Bitwise inclusive OR left to right
    && Logical AND left to right
    || Logical OR left to right
    ? : Ternary operator right to left
    =
    += -=
    *= /=
    %= &=
    ^= |=
    <<= >>=
    Assignment operator
    Addition / subtraction assignment
    Multiplication / division assignment
    Modulus and bitwise assignment
    Bitwise exclusive / inclusive OR assignment
    Bitwise left shift / right shift assignment
    right to left
    , comma operator right to left

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Test Your Knowledge
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.