0% found this document useful (0 votes)
19 views

C Lecture 5 Part 2

The document discusses operator precedence and associativity in C. It explains that operator precedence determines the order of operations when multiple operators are present in an expression. Operators with higher precedence are evaluated first. When operators have the same precedence, associativity (left-to-right or right-to-left) determines the order of evaluation. The document provides a table listing C operators from highest to lowest precedence and their associativity. It also gives examples to illustrate precedence and associativity.

Uploaded by

Gagan
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)
19 views

C Lecture 5 Part 2

The document discusses operator precedence and associativity in C. It explains that operator precedence determines the order of operations when multiple operators are present in an expression. Operators with higher precedence are evaluated first. When operators have the same precedence, associativity (left-to-right or right-to-left) determines the order of evaluation. The document provides a table listing C operators from highest to lowest precedence and their associativity. It also gives examples to illustrate precedence and associativity.

Uploaded by

Gagan
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/ 9

Operator precedence and associativity

1
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.

2
Operator Precedence and Associativity in C
Precedence Operator
Description Associativity

() 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

!,~ Logical NOT, Bitwise complement


2 (type) Cast Operator Right-to-Left

* Dereference Operator
& Addressof Operator
sizeof Determine size in bytes
3
Operator Precedence and Associativity in C

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

4
Operator Precedence and Associativity in C
11 && Logical AND Left-to-Right

12 || Logical OR Left-to-Right

13 ?: Ternary conditional Right-to-Left

= 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

5
Operator Precedence:

• Operator precedence determines which operation is performed first in an


expression with more than one operator with different precedence.

Example: 10 + 20 * 30

Note: As we can see, the expression is evaluated as,10 + (20 * 30) but not as
(10 + 20) * 30 due to * operator having higher precedence.
6
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: 100 / 5 % 2

7
Operator Precedence and Associativity

Operators Precedence and Associativity are two characteristics of operators


that determine the evaluation order of sub-expressions.

Example: exp = 100 + 200 / 10 - 3 * 10

8
Notes to Remember:

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy