Table of Contents

    Operator Precedence in C: Understanding Expression Evaluation

    Operator Precedence in C: Understanding Expression Evaluation

    Operator precedence determines the sequence in which operators in an expression are executed. This order of precedence can affect the result of an expression substantially. For example, suppose you are to process job applications and you want to only accept applicants who are 25 or older and have graduated from Harvard or Yale. Here’s the age condition you can represent by this conditional expression:

    Age >= 25

    Suppose that you represent graduation by the variables Yale and Harvard, which may be true or false. Now you can write the condition as follows:

    Age >= 25 && Harvard || Yale

    Unfortunately, this will result in protest because you will now accept Yale graduates who are under 25. In fact, this statement will accept Yale graduates of any age. But if you’re from Harvard, you must be 25 or over to be accepted. Because of operator precedence, this expression is effectively the following:

    (Age >= 25 && Harvard) || Yale

    So now you take anybody at all from Yale. I’m sure those wearing a Y-front sweatshirt will claim that this is as it should be, but what you really meant was this:

    Age >= 25 && (Harvard || Yale)

    Because of operator precedence, you must put the parentheses in the expression to force the order of operations to be what you want.

    In general, the precedence of the operators in an expression determines whether it is necessary for you to put parentheses in to get the result you want, but if you are unsure of the precedence of the operators you are using, it does no harm to put the parentheses in. Below table shows the order of precedence for all the operators in C, from highest at the top to lowest at the bottom.

    Operator Precedence Table

    Description Operator Associativity
    Function Expression  ( ) Left to Right
    Array Expression [ ]  Left to Right
    Structure operator  -> Left to Right
    Structure operator  . Left to Right
    Unary minus  - Right to left
    Increment/Decrement ++ --  Right to left
    One’s compliment  Right to left
    Negation ! Right to left
    Address of & Right to left
    Value of address  Right to left
    Type cast ( type )  Right to left
    Size in bytes  sizeof Right to left
    Multiplication * Left to Right
    Division / Left to Right
    Modulus % Left to Right
    Addition + Left to Right
    Subtraction - Left to Right
    Left shift   <<  Left to Right
    Right shift  >> Left to Right
    Less than Left to Right
    Less than or equal to <= Left to Right
    Greater than  > Left to Right
    Greater than or equal to >= Left to Right
    Equal to == Left to Right
    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
    Conditional ? : Right to left 
    Assignment 

    *=  /=   %=

    +=  -=  &=

    ^=  |=

    <<=  >>=

    Right to left 

    Right to left 

    Right to left 

    Right to left 

    Right to left 

    Comma  , Right to left 

    For better understanding

    Common operators
    assignment increment
    decrement
    arithmetic logical comparison member
    access
    other

    a = b
    a += b
    a -= b
    a *= b
    a /= b
    a %= b
    a &= b
    a |= b
    a ^= b
    a <<= b
    a >>= b

    ++a
    --a
    a++
    a--

    +a
    -a
    a + b
    a - b
    a * b
    a / b
    a % b
    ~a
    a & b
    a | b
    a ^ b
    a << b
    a >> b

    !a
    a && b
    a || b

    a == b
    a != b
    a < b
    a > b
    a <= b
    a >= b

    a[b]
    *a
    &a
    a->b
    a.b

    a(...)
    a, b
    (type) a
    ? :
    sizeof
    _Alignof (since C11)