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 in c programming
Operators in c programmingOperators in c programming
Operators in c programming
savitamhaske
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
Chukka Nikhil Chakravarthy
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
Manoj Tyagi
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Nested loops
Nested loopsNested loops
Nested loops
Neeru Mittal
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Ratnakar Mikkili
 
strong slot and filler
strong slot and fillerstrong slot and filler
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
Graph algorithm
Graph algorithmGraph algorithm
Graph algorithm
University of Potsdam
 
Pointer in c
Pointer in cPointer in c
Pointer in c
lavanya marichamy
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
Prabhu Govind
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
Tareq Hasan
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
Akshaya Arunan
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
jyoti_lakhani
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Ignaz Wanders
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
Mahender Boda
 
Operators
OperatorsOperators
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
Deepak Singh
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
JAYA
 

What's hot (20)

Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
Inline function
Inline functionInline function
Inline function
 
Nested loops
Nested loopsNested loops
Nested loops
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
strong slot and filler
strong slot and fillerstrong slot and filler
strong slot and filler
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Graph algorithm
Graph algorithmGraph algorithm
Graph algorithm
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Operators
OperatorsOperators
Operators
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 

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

原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
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
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
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
 
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 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
Yasser Mahgoub
 
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
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
edwin408357
 
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
 
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
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
harshapolam10
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
aryanpankaj78
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 

Recently uploaded (20)

原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
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
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
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...
 
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 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
 
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...
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
 
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...
 
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...
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 

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, ©