C Programming Tutorial - ITDeveloper

ITDeveloper : C Programming

C Programming

Step by Step Tutorials


Share with a Friend

Operators Precedence In C

The concept of operator precedence and associativity in C helps in determining which operators will be given priority when there are multiple operators in the expression. It is very common to have multiple operators in C language and the compiler first evaluates the operater with higher precedence. It helps to maintain the ambiguity of the expression and helps us in avoiding unnecessary use of parenthesis.

In this article, we will discuss operator precedence, operator associativity, and precedence table according to which the priority of the operators in expression is decided in C language.

Operator Precedence and Associativity Table

The following tables list the C operator precedence from highest to lowest and the associativity for each of the operators:

   Precedence   

   Operator   
 

Description

   Associativity   

1

()

Parentheses (function call)

Left-to-Right

[]

Array Subscript (Square Brackets)

.

Dot Operator

->

Structure Pointer Operator

++ , —

Postfix increment, decrement

2

++ / —

Prefix increment, decrement

Right-to-Left

+ / –

Unary plus, minus

! , ~

Logical NOT,  Bitwise complement

(type)

Cast Operator

*

Dereference Operator

&

Addressof Operator

sizeof

Determine size in bytes

3

*,/,%

Multiplication, division, modulus

Left-to-Right

4

+/-

Addition, subtraction

Left-to-Right

5

<< , >>

Bitwise shift left, Bitwise shift right

Left-to-Right

6

< , <=

Relational less than, less than or equal to

Left-to-Right

> , >=

Relational greater than, greater than or equal to

7

== , !=

Relational is equal to, is not equal to

Left-to-Right

8

&

Bitwise AND

Left-to-Right

9

^

Bitwise exclusive OR

Left-to-Right

10

|

Bitwise inclusive OR

Left-to-Right

11

&&

Logical AND

Left-to-Right

12

||

Logical OR

Left-to-Right

13

?:

Ternary conditional

Right-to-Left

14

=

Assignment

Right-to-Left

+= , -=

Addition, subtraction assignment

*= , /=

Multiplication, division assignment

%= , &=

Modulus, bitwise AND assignment

^= , |=

Bitwise exclusive, inclusive OR assignment

<<=, >>=

Bitwise shift left, right assignment

15

,

comma (expression separator)

Left-to-Right

Operator Precedence in C

Operator precedence determines which operation is performed first in an expression with more than one operator with different precedence.

Example of Operator Precedence

Let’s try to evaluate the following expression,

10 + 20 * 30

The expression contains two operators, + (plus), and * (multiply). According to the given table, the * has higher precedence than so, the first evaluation will be

10 + (20 * 30)

After evaluating the higher precedence operator, the expression is

10 + 600

Now, the + operator will be evaluated.

// C Program to illustrate operator precedence
#include <stdio.h>
 
int main()
{
    // printing the value of same expression
    printf("10 + 20 * 30 = %d", 10 + 20 * 30);
 
    return 0;
}
 
 
 
Output
10 + 20 * 30 = 610

As we can see, the expression is evaluated as,10 + (20 * 30) but not as (10 + 20) * 30 due to * operator having higher precedence.

Operator Associativity

Operator associativity is used when two operators of the same precedence appear in an expression. Associativity can be either from Left to Right or Right to Left. 

Example of Operator Associativity

Let’s evaluate the following expression,

100 / 5 % 2

Both / (division) and % (Modulus) operators have the same precedence, so the order of evaluation will be decided by associativity.

According to the given table, the associativity of the multiplicative operators is from Left to Right. So,

(100 / 5) % 2

After evaluation, the expression will be

20 % 2

Now, the % operator will be evaluated.

0


We can verify the above using the following C program:


// C Program to illustrate operator Associativity
#include <stdio.h>
 
int main()
{
    // Verifying the result of the same expression
    printf("100 / 5 % 2 = %d", 100 / 5 % 2);
 
    return 0;
}
Output
100 / 5 % 2 = 0

Operators Precedence and Associativity are two characteristics of operators that determine the evaluation order of sub-expressions.

Example of Operator Precedence and Associativity

In general, the concept of precedence and associativity is applied together in expressions. So let’s consider an expression where we have operators with various precedence and associativity

exp = 100 + 200 / 10 - 3 * 10

Here, we have four operators, in which the and * operators have the same precedence but have higher precedence than the and  operators. So, according to the Left-to-Right associativity of / and */ will be evaluated first.

exp = 100 + (200 / 10) - 3 * 10
    = 100 + 20 - 3 * 10

After that, * will be evaluated,

exp = 100 + 20 - (3 * 10)
    = 100 + 20 - 30

Now, between + and + will be evaluated due to Left-to-Right associativity.

exp = (100 + 20) - 30
    = 120 - 30

At last,  will be evaluated.

exp = 120 - 30
    = 90


Again, we can verify this using the following C program.


// C Program to illustrate the precedence and associativity
// of the operators in an expression
#include <stdio.h>
 
 
int main()
{
    // getting the result of the same expression as the
    // example
    int exp = 100 + 200 / 10 - 3 * 10;
    printf("100 + 200 / 10 - 3 * 10 = %d", exp);
return 0;
}

Output:

100 + 200 / 10 – 3 * 10 = 90

NOTE:

There are a few important points and cases that we need to remember for operator associativity and precedence which are as follows:

1. Associativity is only used when there are two or more operators of the same precedence.

The point to note is associativity doesn’t define the order in which operands of a single operator are evaluated. For example, consider the following program, associativity of the + operator is left to right, but it doesn’t mean f1() is always called before f2(). The output of the following program is in-fact compiler-dependent.

Example

C

// Associativity is not used in the below program.
// Output is compiler dependent.
 
#include <stdio.h>
 
int x = 0;
int f1()
{
    x = 5;
    return x;
}
int f2()
{
    x = 10;
    return x;
}
int main()
{
    int p = f1() + f2();
    printf("%d ", x);
    return 0;
}
Output
10