SlideShare a Scribd company logo
1 of 14
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

Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming Kamal Acharya
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements Tarun Sharma
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c languagesneha2494
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++Hitesh Kumar
 
Control structure C++
Control structure C++Control structure C++
Control structure C++Anil Kumar
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programmingprogramming9
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
 

What's hot (20)

Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming
 
Inline function
Inline functionInline function
Inline function
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
C functions
C functionsC functions
C functions
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Interpreter
InterpreterInterpreter
Interpreter
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 

Similar to C Operators, Expressions Guide

Similar to C Operators, Expressions Guide (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
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
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++
 

Recently uploaded

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 

Recently uploaded (20)

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 

C Operators, Expressions Guide

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