SlideShare a Scribd company logo
C OPERATORS, OPERANDS, EXPRESSION &
STATEMENTS
 Operators are symbols which take one or more operands or expressions
and perform arithmetic or logical computations.
 Operands are variables or expressions which are used in conjunction
with operators to evaluate the expression.
 Combination of operands and operators form an expression.
 Expressions are sequences of operators, operands, and punctuators that
specify a computation.
 Evaluation of expressions is based on the operators that the expressions
contain and the context in which they are used.
 Expression can result in a value and can produce side effects.
 A side effect is a change in the state of the execution environment.
C OPERATORS
 An expression is any valid set of literals, variables,
operators, operands and expressions that evaluates to a
single value.
 This value can be a number, a string or a logical value.
 For instance a = b + c; denotes an expression in which
there are 3 operands a, b, c and two operator + and =.
 A statement, the smallest independent computational
unit, specifies an action to be performed.
 In most cases, statements are executed in sequence.
 The number of operands of an operator is called its arity.
 Based on arity, operators are classified as nullary (no
operands), unary (1 operand), binary (2 operands),
ternary (3 operands).
3/46
www.tenouk.com, ©
OPERATOR PRECEDENCE
 Consider the following arithmetic operation:
- left to right
6 / 2 * 1 + 2 = 5
- right to left
6/2 * 1 + 2 = 1
- using parentheses
= 6 / (2 * 1) + 2
= (6 / 2) + 2
= 3 + 2
= 5
34/46
Inconsistent
answers!
www.tenouk.com, ©
OPERATOR PRECEDENCE
 Operator precedence: a rule used to clarify
unambiguously which operations (operator
and operands) should be performed first in
the given (mathematical) expression.
 Use precedence levels that conform to the
order commonly used in mathematics.
 However, parentheses take the highest
precedence and operation performed from
the innermost to the outermost parentheses.
35/46
www.tenouk.com, ©
OPERATOR PRECEDENCE
 Precedence and associativity of C operators
affect the grouping and evaluation of
operands in expressions.
 Is meaningful only if other operators with
higher or lower precedence are present.
 Expressions with higher-precedence
operators are evaluated first.
36/46
www.tenouk.com, ©
Precedence and Associativity of C Operators
Symbol Type of Operation Associativity
[ ] ( ) . –> postfix ++ and postfix –– Expression Left to right
prefix ++ and prefix –– sizeof
& * + – ~ !
Unary Right to left
typecasts Unary Right to left
* / % Multiplicative Left to right
+ – Additive Left to right
<< >> Bitwise shift Left to right
< > <= >= Relational Left to right
== != Equality 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-expression Right to left
= *= /= %=
+= –= <<= >>= &=
^= |=
Simple and compound
assignment
Right to left
, Sequential evaluation Left to right
OPERATOR PRECEDENCE
37/46
www.tenouk.com, ©
OPERATOR PRECEDENCE
 The precedence and associativity (the order in
which the operands are evaluated) of C
operators.
 In the order of precedence from highest to
lowest.
 If several operators appear together, they have
equal precedence and are evaluated according
to their associativity.
 All simple and compound-assignment operators
have equal precedence.
38/46
www.tenouk.com, ©
OPERATOR PRECEDENCE
 Operators with equal precedence such as +
and -, evaluation proceeds according to
the associativity of the operator, either
from right to left or from left to right.
 The direction of evaluation does not affect
the results of expressions that include
more than one multiplication (*), addition
(+), or binary-bitwise (& | ^) operator at
the same level.
39/46
www.tenouk.com, ©
OPERATOR PRECEDENCE
 e.g:
 Order of operations is not defined by the language.
 The compiler is free to evaluate such expressions in
any order, if the compiler can guarantee a consistent
result.
40/46
3 + 5 + (3 + 2) = 13 – right to left
(3 + 5) + 3 + 2 = 13 – left to right
3 + (5 + 3) + 2 = 13 – from middle
www.tenouk.com, ©
OPERATOR PRECEDENCE
 Only the sequential-evaluation (,), logical-AND (&&),
logical-OR (||), conditional-expression (? :), and
function-call operators constitute sequence points
and therefore guarantee a particular order of
evaluation for their operands.
 The sequential-evaluation operator (,) is guaranteed
to evaluate its operands from left to right.
 The comma operator in a function call is not the
same as the sequential-evaluation operator and does
not provide any such guarantee.
41/46
www.tenouk.com, ©
OPERATOR PRECEDENCE
 Logical operators also guarantee evaluation of
their operands from left to right.
 But, they evaluate the smallest number of
operands needed to determine the result of the
expression.
 This is called "short-circuit" evaluation.
 Thus, some operands of the expression may not
be evaluated.
42/46
www.tenouk.com, ©
OPERATOR PRECEDENCE
 For example:
 The second operand, y++, is evaluated
only if x is true (nonzero).
 Thus, y is not incremented if x is false
(0).
43/46
x && y++
www.tenouk.com, ©
 Label the execution order for the following expressions
OPERATOR PRECEDENCE
44/46
www.tenouk.com, ©
a.(rate*rate) + delta
b.2*(salary + bonus)
c. 1/(time + (3*mass))
d.(a - 7) / (t + (9 * v))
OPERATOR PRECEDENCE
45/46
 Convert the following operations to C
expression
www.tenouk.com, ©

More Related Content

What's hot

Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
Operators
OperatorsOperators
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variables
sangrampatil81
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
vishaljot_kaur
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
Chukka Nikhil Chakravarthy
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
Nitin Jawla
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
Prof. Dr. K. Adisesha
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
programming9
 
Pass by value and pass by reference
Pass by value and pass by reference Pass by value and pass by reference
Pass by value and pass by reference
TurnToTech
 
PYTHON NOTES
PYTHON NOTESPYTHON NOTES
PYTHON NOTES
Ni
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
Thooyavan Venkatachalam
 
Character set of c
Character set of cCharacter set of c
Character set of c
Chandrapriya Rediex
 
Basic theorems and properties of boolean algebra
Basic theorems and properties of boolean algebraBasic theorems and properties of boolean algebra
Basic theorems and properties of boolean algebra
Hanu Kavi
 
Characteristics of oop
Characteristics of oopCharacteristics of oop
Characteristics of oop
Rasim Izhar Ali
 
Type conversion
Type conversionType conversion
Type conversion
Frijo Francis
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
Data types
Data typesData types
Data types
Nokesh Prabhakar
 

What's hot (20)

Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Operators
OperatorsOperators
Operators
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variables
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Pass by value and pass by reference
Pass by value and pass by reference Pass by value and pass by reference
Pass by value and pass by reference
 
PYTHON NOTES
PYTHON NOTESPYTHON NOTES
PYTHON NOTES
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Character set of c
Character set of cCharacter set of c
Character set of c
 
Basic theorems and properties of boolean algebra
Basic theorems and properties of boolean algebraBasic theorems and properties of boolean algebra
Basic theorems and properties of boolean algebra
 
Characteristics of oop
Characteristics of oopCharacteristics of oop
Characteristics of oop
 
Type conversion
Type conversionType conversion
Type conversion
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Data types
Data typesData types
Data types
 

Similar to Operator precedence and associativity

C operator and expression
C operator and expressionC operator and expression
C operator and expression
LavanyaManokaran
 
cprogrammingoperator.ppt
cprogrammingoperator.pptcprogrammingoperator.ppt
cprogrammingoperator.ppt
georgejustymirobi1
 
C# operators
C# operatorsC# operators
Cprogrammingoperator
CprogrammingoperatorCprogrammingoperator
Cprogrammingoperator
teach4uin
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
bajiajugal
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
Praveen M Jigajinni
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
Simplilearn
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
sotlsoc
 
C PRESENTATION.pptx
C PRESENTATION.pptxC PRESENTATION.pptx
C PRESENTATION.pptx
VAIBHAV175947
 
operat in vb .pptx
operat in vb .pptxoperat in vb .pptx
operat in vb .pptx
classall
 
C operators
C operatorsC operators
C operators
GPERI
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
Anusuya123
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
sanya6900
 
c programming2.pptx
c programming2.pptxc programming2.pptx
c programming2.pptx
YuvarajuCherukuri
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
Mohammad Imam Hossain
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
Jasleen Kaur (Chandigarh University)
 
operators in c++
operators in c++operators in c++
operators in c++
Kartik Fulara
 
operators in c++
operators in c++operators in c++
operators in c++
Kartik Fulara
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
Kathirvel Ayyaswamy
 

Similar to Operator precedence and associativity (20)

C operator and expression
C operator and expressionC operator and expression
C operator and expression
 
cprogrammingoperator.ppt
cprogrammingoperator.pptcprogrammingoperator.ppt
cprogrammingoperator.ppt
 
C# operators
C# operatorsC# operators
C# operators
 
Cprogrammingoperator
CprogrammingoperatorCprogrammingoperator
Cprogrammingoperator
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
 
C PRESENTATION.pptx
C PRESENTATION.pptxC PRESENTATION.pptx
C PRESENTATION.pptx
 
operat in vb .pptx
operat in vb .pptxoperat in vb .pptx
operat in vb .pptx
 
C operators
C operatorsC operators
C operators
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
 
c programming2.pptx
c programming2.pptxc programming2.pptx
c programming2.pptx
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
operators in c++
operators in c++operators in c++
operators in c++
 
operators in c++
operators in c++operators in c++
operators in c++
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 

Recently uploaded

LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
edwin408357
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
upoux
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
Gas agency management system project report.pdf
Gas agency management system project report.pdfGas agency management system project report.pdf
Gas agency management system project report.pdf
Kamal Acharya
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
ycwu0509
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
PKavitha10
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
PIMR BHOPAL
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
Prakhyath Rai
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
mahaffeycheryld
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 

Recently uploaded (20)

LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
Gas agency management system project report.pdf
Gas agency management system project report.pdfGas agency management system project report.pdf
Gas agency management system project report.pdf
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 

Operator precedence and associativity

  • 1. C OPERATORS, OPERANDS, EXPRESSION & STATEMENTS  Operators are symbols which take one or more operands or expressions and perform arithmetic or logical computations.  Operands are variables or expressions which are used in conjunction with operators to evaluate the expression.  Combination of operands and operators form an expression.  Expressions are sequences of operators, operands, and punctuators that specify a computation.  Evaluation of expressions is based on the operators that the expressions contain and the context in which they are used.  Expression can result in a value and can produce side effects.  A side effect is a change in the state of the execution environment.
  • 2. C OPERATORS  An expression is any valid set of literals, variables, operators, operands and expressions that evaluates to a single value.  This value can be a number, a string or a logical value.  For instance a = b + c; denotes an expression in which there are 3 operands a, b, c and two operator + and =.  A statement, the smallest independent computational unit, specifies an action to be performed.  In most cases, statements are executed in sequence.  The number of operands of an operator is called its arity.  Based on arity, operators are classified as nullary (no operands), unary (1 operand), binary (2 operands), ternary (3 operands). 3/46 www.tenouk.com, ©
  • 3. OPERATOR PRECEDENCE  Consider the following arithmetic operation: - left to right 6 / 2 * 1 + 2 = 5 - right to left 6/2 * 1 + 2 = 1 - using parentheses = 6 / (2 * 1) + 2 = (6 / 2) + 2 = 3 + 2 = 5 34/46 Inconsistent answers! www.tenouk.com, ©
  • 4. OPERATOR PRECEDENCE  Operator precedence: a rule used to clarify unambiguously which operations (operator and operands) should be performed first in the given (mathematical) expression.  Use precedence levels that conform to the order commonly used in mathematics.  However, parentheses take the highest precedence and operation performed from the innermost to the outermost parentheses. 35/46 www.tenouk.com, ©
  • 5. OPERATOR PRECEDENCE  Precedence and associativity of C operators affect the grouping and evaluation of operands in expressions.  Is meaningful only if other operators with higher or lower precedence are present.  Expressions with higher-precedence operators are evaluated first. 36/46 www.tenouk.com, ©
  • 6. Precedence and Associativity of C Operators Symbol Type of Operation Associativity [ ] ( ) . –> postfix ++ and postfix –– Expression Left to right prefix ++ and prefix –– sizeof & * + – ~ ! Unary Right to left typecasts Unary Right to left * / % Multiplicative Left to right + – Additive Left to right << >> Bitwise shift Left to right < > <= >= Relational Left to right == != Equality 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-expression Right to left = *= /= %= += –= <<= >>= &= ^= |= Simple and compound assignment Right to left , Sequential evaluation Left to right OPERATOR PRECEDENCE 37/46 www.tenouk.com, ©
  • 7. OPERATOR PRECEDENCE  The precedence and associativity (the order in which the operands are evaluated) of C operators.  In the order of precedence from highest to lowest.  If several operators appear together, they have equal precedence and are evaluated according to their associativity.  All simple and compound-assignment operators have equal precedence. 38/46 www.tenouk.com, ©
  • 8. OPERATOR PRECEDENCE  Operators with equal precedence such as + and -, evaluation proceeds according to the associativity of the operator, either from right to left or from left to right.  The direction of evaluation does not affect the results of expressions that include more than one multiplication (*), addition (+), or binary-bitwise (& | ^) operator at the same level. 39/46 www.tenouk.com, ©
  • 9. OPERATOR PRECEDENCE  e.g:  Order of operations is not defined by the language.  The compiler is free to evaluate such expressions in any order, if the compiler can guarantee a consistent result. 40/46 3 + 5 + (3 + 2) = 13 – right to left (3 + 5) + 3 + 2 = 13 – left to right 3 + (5 + 3) + 2 = 13 – from middle www.tenouk.com, ©
  • 10. OPERATOR PRECEDENCE  Only the sequential-evaluation (,), logical-AND (&&), logical-OR (||), conditional-expression (? :), and function-call operators constitute sequence points and therefore guarantee a particular order of evaluation for their operands.  The sequential-evaluation operator (,) is guaranteed to evaluate its operands from left to right.  The comma operator in a function call is not the same as the sequential-evaluation operator and does not provide any such guarantee. 41/46 www.tenouk.com, ©
  • 11. OPERATOR PRECEDENCE  Logical operators also guarantee evaluation of their operands from left to right.  But, they evaluate the smallest number of operands needed to determine the result of the expression.  This is called "short-circuit" evaluation.  Thus, some operands of the expression may not be evaluated. 42/46 www.tenouk.com, ©
  • 12. OPERATOR PRECEDENCE  For example:  The second operand, y++, is evaluated only if x is true (nonzero).  Thus, y is not incremented if x is false (0). 43/46 x && y++ www.tenouk.com, ©
  • 13.  Label the execution order for the following expressions OPERATOR PRECEDENCE 44/46 www.tenouk.com, ©
  • 14. a.(rate*rate) + delta b.2*(salary + bonus) c. 1/(time + (3*mass)) d.(a - 7) / (t + (9 * v)) OPERATOR PRECEDENCE 45/46  Convert the following operations to C expression www.tenouk.com, ©