0% found this document useful (0 votes)
56 views4 pages

Operator Precedence and Associativity in C

The document explains operator precedence and associativity in C programming, detailing how expressions with multiple operators are evaluated based on their priority. It includes a comprehensive table listing operators from highest to lowest precedence and their associativity. Additionally, it provides examples to illustrate the concepts of precedence and associativity, along with important points to remember.

Uploaded by

Shahla Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views4 pages

Operator Precedence and Associativity in C

The document explains operator precedence and associativity in C programming, detailing how expressions with multiple operators are evaluated based on their priority. It includes a comprehensive table listing operators from highest to lowest precedence and their associativity. Additionally, it provides examples to illustrate the concepts of precedence and associativity, along with important points to remember.

Uploaded by

Shahla Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Operator Precedence and Associativity 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 operator
with higher precedence. It helps to maintain the ambiguity of the expression and helps us in
avoiding unnecessary use of parenthesis.
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:

Operator
Precedence Associativity
Description

() Parentheses (function call)

[] Array Subscript (Square Brackets)

1 . Dot Operator Left-to-Right

-> Structure Pointer Operator

++ , — Postfix increment, decrement

++ / — Prefix increment, decrement

+/– Unary plus, minus


2 Right-to-Left
!,~ Logical NOT, Bitwise complement

(type) Cast Operator


Operator
Precedence Associativity
Description

* 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

< , <= Relational less than, less than or equal to

6 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


Operator
Precedence Associativity
Description

= Assignment

+= , -= Addition, subtraction assignment

*= , /= Multiplication, division assignment

14 Right-to-Left
%= , &= 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,
5+20*15

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

5+(20*15)

After evaluating the higher precedence operator, the expression is

5+300
Now, the + operator will be evaluated .

305

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 % 4

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)%4
After evaluation, the expression will be
20%4

Now, the % operator will be evaluated .

Important Points
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.
2. We can use parenthesis to change the order of evaluation
3. All operators with the same precedence have the same associativity.
4. Precedence and associativity of postfix ++ and prefix ++ are different.
5. Comma has the least precedence among all operators and should be used carefully.
6. There is no chaining of comparison operators in C

You might also like