SlideShare une entreprise Scribd logo
1  sur  19
Operators & Expressions
Operators
• C supports rich set of operators.
• An operator is a symbol that tells the computer to
perform certain mathematical or logical manipulations.
• Operators are used in programs to manipulate data and
variables.
Types of ‘C’ operators
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and Decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
1. Arithmetic operator
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo division
2. Relational operator
C supports six Relational Operators
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
3.Logical operators
• Logical Operators
– &&, || and ! are the three logical operators.
– expr1 && expr2 has a value 1 if expr1 and expr2 both are
nonzero.
– expr1 || expr2 has a value 1 if expr1 and expr2 both are
nonzero.
– !expr1 has a value 1 if expr1 is zero else 0.
– Example
– if ( marks >= 40 && attendance >= 75 ) grade = ‘P’
– If ( marks < 40 || attendance < 75 ) grade = ‘N’
4. Assignment operators
• Assignment operators are used to assign the result of an
expression to a variable.
• C has a set of ‘shorthand’ assignment operator (op=):
v op =exp;
Exam - a += 3 ;
a = a + 3 ;
Both are same.
Shorthand Assignment operators
Simple assignment operator
Shorthand operator
a = a+1 a + =1
a = a-1 a - =1
a = a* (m+n) a * = m+n
a = a / (m+n) a / = m+n
a = a %b a %=b
5. Increment and decrement operators.
• Increment Operator ++
a=10;
a++ or a+1=11;
• Decrement Operator --
b=5;
b-- or b-1=4;
• Similarly increment and decrement operator is used in
subscripted variables as:
a[ i++]=5;
is equivalent to
a[ i]=5;
i=i+1;
6. Conditional operator
• C supports a ternary operator i.e. conditional operator.
• The conditional operator is defined as
“ ? : ”
• The conditional operator works in following format:
for example:
a=11;
b=20;
x=(a>b) ? a : b;
7. Bitwise operator
• C supports bitwise operators for manipulation of data at bit
level.
• Bitwise operators may not be applied to float or double.
• Bitwise operators are as follows:
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left
>> shift right
~ One’s Complements
8. Special operator
• C supports some special operators such as:
comma operator “,”
x=(a=5,b=6)
size of operator “sizeof()”
Address operator “&”
pointer operator “*”
member selection operator “. and -> ”
More about operators
• Unary operator
• Binary operator
• Ternary operator
Precedence of operators
• Precedence establishes the hierarchy of one set of operators
over another when an arithmetic expression has to be
evaluated.
• It refers to the order in which c evaluates operators.
• The evaluation of operators in an arithmetic
expression takes place from left to right for operators having
equal precedence .
Exam. k = 2 * 3/4 + 4/4 + 8–2 + 5/8 ;
O/P => 8
Precedence of operators
BODMAS RULE-
Brackets of Division Multiplication Addition Subtraction
Brackets will have the highest precedence and have to be evaluated
first, then comes of , then comes division, multiplication, addition
and finally subtraction.
C language uses some rules in evaluating the expressions and they r
called as precedence rules or sometimes also referred to as
hierarchy of operations, with some operators with highest
precedence and some with least.
The 2 distinct priority levels of arithmetic operators in c are-
Highest priority : * / %
Lowest priority : + -
Associativity of operators
• Associativity tells how an operator associates with its operands.
for eg:
1. The unary minus associates minus with the quantity to its right.
2. The assignment operator = associates from right to left.
• Hence the expression on the right is evaluated first and its value is
assigned to the variable on the left.
• Associativity also refers to the order in which c evaluates operators in an
expression having same precedence.
• Such type of operator can operate either left to right or vice versa.
• The operator () function call has highest precedence & the comma
operator has lowest precedence
• All unary , conditional & assignment operators associate RIGHT TO
LEFT .
• All other remaining operators associate LEFT TO RIGHT
Rules for evaluation of expression
1. First parenthesized sub expression from left to right are
evaluated.
2. If parentheses are nested, the evaluation begins with the
innermost sub expression
3. The precedence rule is applied in determining the order of
application of operators in evaluating sub expressions
4. The associatively rule is applied when 2 or more operators of
the same precedence level appear in a sub expression.
5. Arithmetic expressions are evaluated from left to right using
the rules of precedence
6. When parentheses are used, the expressions within parentheses
assume highest priority
Hierarchy of operators
Operator Description Associativity
( ), [ ] Function call, array element
reference
Left to Right
+, -, ++, - -
,!,~,*,&
Unary plus, minus, increment,
decrement, logical negation,
1’s complement, pointer
reference, address
Right to Left
*, / , % Multiplication, division,
modulus
Left to Right
Reference:
Presentation: Operators & Expressions
Published by: Godwin Robinson
URL: https://slideplayer.com/slide/9355540/

Contenu connexe

Tendances (20)

Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
Function in C program
Function in C programFunction in C program
Function in C program
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
Operators in c language
Operators in c languageOperators in c language
Operators in c language
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Function in C
Function in CFunction in C
Function in C
 
C functions
C functionsC functions
C functions
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Inline function
Inline functionInline function
Inline function
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 

Similaire à Operators and Expressions (20)

OPERATORS OF C++
OPERATORS OF C++OPERATORS OF C++
OPERATORS OF C++
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
IOS Swift Language 3rd tutorial
IOS Swift Language 3rd tutorialIOS Swift Language 3rd tutorial
IOS Swift Language 3rd tutorial
 
C Operators
C OperatorsC Operators
C Operators
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
Coper in C
Coper in CCoper in C
Coper in C
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
Operators
OperatorsOperators
Operators
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
Chap 3(operator expression)
Chap 3(operator expression)Chap 3(operator expression)
Chap 3(operator expression)
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
 
Python Programming | JNTUK | UNIT 1 | Lecture 5
Python Programming | JNTUK | UNIT 1 | Lecture 5Python Programming | JNTUK | UNIT 1 | Lecture 5
Python Programming | JNTUK | UNIT 1 | Lecture 5
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 
C++
C++ C++
C++
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
L3 operators
L3 operatorsL3 operators
L3 operators
 

Plus de Munazza-Mah-Jabeen (20)

Virtual Functions
Virtual FunctionsVirtual Functions
Virtual Functions
 
The Standard Template Library
The Standard Template LibraryThe Standard Template Library
The Standard Template Library
 
Object-Oriented Software
Object-Oriented SoftwareObject-Oriented Software
Object-Oriented Software
 
Templates and Exceptions
 Templates and Exceptions Templates and Exceptions
Templates and Exceptions
 
Dictionaries and Sets
Dictionaries and SetsDictionaries and Sets
Dictionaries and Sets
 
More About Strings
More About StringsMore About Strings
More About Strings
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
 
Lists and Tuples
Lists and TuplesLists and Tuples
Lists and Tuples
 
Files and Exceptions
Files and ExceptionsFiles and Exceptions
Files and Exceptions
 
Functions
FunctionsFunctions
Functions
 
Pointers
PointersPointers
Pointers
 
Repitition Structure
Repitition StructureRepitition Structure
Repitition Structure
 
Inheritance
InheritanceInheritance
Inheritance
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Arrays and Strings
Arrays and StringsArrays and Strings
Arrays and Strings
 
Objects and Classes
Objects and ClassesObjects and Classes
Objects and Classes
 
Functions
FunctionsFunctions
Functions
 
Structures
StructuresStructures
Structures
 
Loops and Decisions
Loops and DecisionsLoops and Decisions
Loops and Decisions
 

Dernier

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 

Dernier (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

Operators and Expressions

  • 2. Operators • C supports rich set of operators. • An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. • Operators are used in programs to manipulate data and variables.
  • 3. Types of ‘C’ operators 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operators 5. Increment and Decrement operators 6. Conditional operators 7. Bitwise operators 8. Special operators
  • 4. 1. Arithmetic operator + Addition - Subtraction * Multiplication / Division % Modulo division
  • 5. 2. Relational operator C supports six Relational Operators < Is less than <= Is less than or equal to > Is greater than >= Is greater than or equal to == Is equal to != Is not equal to
  • 6. 3.Logical operators • Logical Operators – &&, || and ! are the three logical operators. – expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero. – expr1 || expr2 has a value 1 if expr1 and expr2 both are nonzero. – !expr1 has a value 1 if expr1 is zero else 0. – Example – if ( marks >= 40 && attendance >= 75 ) grade = ‘P’ – If ( marks < 40 || attendance < 75 ) grade = ‘N’
  • 7. 4. Assignment operators • Assignment operators are used to assign the result of an expression to a variable. • C has a set of ‘shorthand’ assignment operator (op=): v op =exp; Exam - a += 3 ; a = a + 3 ; Both are same.
  • 8. Shorthand Assignment operators Simple assignment operator Shorthand operator a = a+1 a + =1 a = a-1 a - =1 a = a* (m+n) a * = m+n a = a / (m+n) a / = m+n a = a %b a %=b
  • 9. 5. Increment and decrement operators. • Increment Operator ++ a=10; a++ or a+1=11; • Decrement Operator -- b=5; b-- or b-1=4; • Similarly increment and decrement operator is used in subscripted variables as: a[ i++]=5; is equivalent to a[ i]=5; i=i+1;
  • 10. 6. Conditional operator • C supports a ternary operator i.e. conditional operator. • The conditional operator is defined as “ ? : ” • The conditional operator works in following format: for example: a=11; b=20; x=(a>b) ? a : b;
  • 11. 7. Bitwise operator • C supports bitwise operators for manipulation of data at bit level. • Bitwise operators may not be applied to float or double. • Bitwise operators are as follows: & bitwise AND | bitwise OR ^ bitwise exclusive OR << shift left >> shift right ~ One’s Complements
  • 12. 8. Special operator • C supports some special operators such as: comma operator “,” x=(a=5,b=6) size of operator “sizeof()” Address operator “&” pointer operator “*” member selection operator “. and -> ”
  • 13. More about operators • Unary operator • Binary operator • Ternary operator
  • 14. Precedence of operators • Precedence establishes the hierarchy of one set of operators over another when an arithmetic expression has to be evaluated. • It refers to the order in which c evaluates operators. • The evaluation of operators in an arithmetic expression takes place from left to right for operators having equal precedence . Exam. k = 2 * 3/4 + 4/4 + 8–2 + 5/8 ; O/P => 8
  • 15. Precedence of operators BODMAS RULE- Brackets of Division Multiplication Addition Subtraction Brackets will have the highest precedence and have to be evaluated first, then comes of , then comes division, multiplication, addition and finally subtraction. C language uses some rules in evaluating the expressions and they r called as precedence rules or sometimes also referred to as hierarchy of operations, with some operators with highest precedence and some with least. The 2 distinct priority levels of arithmetic operators in c are- Highest priority : * / % Lowest priority : + -
  • 16. Associativity of operators • Associativity tells how an operator associates with its operands. for eg: 1. The unary minus associates minus with the quantity to its right. 2. The assignment operator = associates from right to left. • Hence the expression on the right is evaluated first and its value is assigned to the variable on the left. • Associativity also refers to the order in which c evaluates operators in an expression having same precedence. • Such type of operator can operate either left to right or vice versa. • The operator () function call has highest precedence & the comma operator has lowest precedence • All unary , conditional & assignment operators associate RIGHT TO LEFT . • All other remaining operators associate LEFT TO RIGHT
  • 17. Rules for evaluation of expression 1. First parenthesized sub expression from left to right are evaluated. 2. If parentheses are nested, the evaluation begins with the innermost sub expression 3. The precedence rule is applied in determining the order of application of operators in evaluating sub expressions 4. The associatively rule is applied when 2 or more operators of the same precedence level appear in a sub expression. 5. Arithmetic expressions are evaluated from left to right using the rules of precedence 6. When parentheses are used, the expressions within parentheses assume highest priority
  • 18. Hierarchy of operators Operator Description Associativity ( ), [ ] Function call, array element reference Left to Right +, -, ++, - - ,!,~,*,& Unary plus, minus, increment, decrement, logical negation, 1’s complement, pointer reference, address Right to Left *, / , % Multiplication, division, modulus Left to Right
  • 19. Reference: Presentation: Operators & Expressions Published by: Godwin Robinson URL: https://slideplayer.com/slide/9355540/