SlideShare a Scribd company logo
Department of FOT
By
Prof. Premavathi T
Unit - 6
Structure and union
01CE0101 - Computer
Programming
Structures
Structure
Arrays allow to define type of variables
that can hold several data items of the
same kind.
Similarly structure is another user
defined data type available in C that
allows to combine data items of different
kinds.
Structures are used to represent a record.
Defining a
Structure
 To define a structure, you must use
the struct statement.
 The struct statement defines a new data type,
with more than one member.
 The format of the struct statement is as follows
Example
Accessing
Structure
Members
To access any member of a structure, we use
the member access operator (.)
The member access operator is coded as a
period between the structure variable name
and the structure member that we wish to
access.
You would use the keyword struct to define
variables of structure type.
Example
Program
Example
Program
Program
output
Structures
as Function
Arguments
You can pass a structure as a function
argument in the same way as you pass any
other variable or pointer.
Union
Union
A union is a special data type available in
C that allows to store different data types
in the same memory location.
 You can define a union with many
members, but only one member can
contain a value at any given time.
 Unions provide an efficient way of using
the same memory location for multiple-
purpose.
Defining a
Union
To define a union, you must use
the union statement in the same way as
you did while defining a structure.
The union statement defines a new data
type with more than one member for
your program.
Defining a
Union
The format of the union statement is as
follows
Each member definition is a normal variable
definition, such as int i; or float f; or any
other valid variable definition.
At the end of the union's definition, before
the final semicolon, you can specify one or
more union variables but it is optional.
Defining a
Union
• Now, a variable of Data type can store an
integer, a floating-point number, or a string
of characters.
• It means a single variable, i.e., same
memory location, can be used to store
multiple types of data.
Example
Accessing
Union
Members
To access any member of a union, we
use the member access operator (.).
 The member access operator is coded
as a period between the union variable
name and the union member that we
wish to access.
You would use the keyword union to
define variables of union type.
Example
program
Difference
between
structure and
union
Advantages
of structure
• Structures gather more than one piece of data
about the same subject together in the same place.
• It is helpful when you want to gather the data of
similar data types and parameters like first name,
last name, etc.
• It is very easy to maintain as we can represent the
whole record by using a single name.
• In structure, we can pass complete set of records to
any function using a single parameter.
• You can use an array of structure to store more
records with similar types.
Disadvantag
es of
structure
 Change of one data structure in a code necessitates changes
at many other places.Therefore, the changes become hard
to track.
 Structure is slower because it requires storage space for all
the data.
 You can retrieve any member at a time in structure
whereas you can access one member at a time in the union.
 Structure occupies space for each and every member
written in inner parameters while union occupies space for a
member having the highest size written in inner parameters.
 Structure supports flexible array. Union does not support a
flexible array.
Advantages
of union
•It occupies less memory compared to
structure.
•When you use union, only the last variable
can be directly accessed.
•Union is used when you have to use the same
memory location for two or more data
members.
•It enables you to hold data of only one data
member.
•Its allocated space is equal to maximum size
of the data member.
Disadvantage
s of union
•You can use only one union
member at a time.
•All the union variables cannot
be initialized or used with
varying values at a time.
•Union assigns one common
storage space for all its
members.
That’s All….
Thank you!!!

More Related Content

Similar to Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx

Unions.pptx
Unions.pptxUnions.pptx
Unions.pptx
OluwafolakeOjo
 
Unit 3
Unit 3Unit 3
Unit 3
TPLatchoumi
 
Programming in C
Programming in CProgramming in C
Programming in C
MalathiNagarajan20
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
Rohit Shrivastava
 
Structures unions
Structures  unionsStructures  unions
Structures unions
Sugnan M
 
C- language Lecture 7
C- language Lecture 7C- language Lecture 7
C- language Lecture 7
Hatem Abd El-Salam
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
Tanmay Modi
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
tanmaymodi4
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
Swarup Boro
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
Swarup Kumar Boro
 
C programming session7
C programming  session7C programming  session7
C programming session7
Keroles karam khalil
 
C programming session7
C programming  session7C programming  session7
C programming session7
Keroles karam khalil
 
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
ssuser5610081
 
Technical Interview
Technical InterviewTechnical Interview
Technical Interview
prashant patel
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
eShikshak
 
UNION 1.pptx
UNION 1.pptxUNION 1.pptx
UNION 1.pptx
FarhanAbdullah29
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
Selvaraj Seerangan
 
object oriented analysis data.pptx
object oriented analysis data.pptxobject oriented analysis data.pptx
object oriented analysis data.pptx
nibiganesh
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
Sowri Rajan
 
Data models
Data modelsData models
Data models
Hira Bukhari
 

Similar to Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx (20)

Unions.pptx
Unions.pptxUnions.pptx
Unions.pptx
 
Unit 3
Unit 3Unit 3
Unit 3
 
Programming in C
Programming in CProgramming in C
Programming in C
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
Structures unions
Structures  unionsStructures  unions
Structures unions
 
C- language Lecture 7
C- language Lecture 7C- language Lecture 7
C- language Lecture 7
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
 
Technical Interview
Technical InterviewTechnical Interview
Technical Interview
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
 
UNION 1.pptx
UNION 1.pptxUNION 1.pptx
UNION 1.pptx
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
object oriented analysis data.pptx
object oriented analysis data.pptxobject oriented analysis data.pptx
object oriented analysis data.pptx
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Data models
Data modelsData models
Data models
 

Recently uploaded

Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 

Recently uploaded (20)

Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 

Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx

  • 1. Department of FOT By Prof. Premavathi T Unit - 6 Structure and union 01CE0101 - Computer Programming
  • 3. Structure Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available in C that allows to combine data items of different kinds. Structures are used to represent a record.
  • 4. Defining a Structure  To define a structure, you must use the struct statement.  The struct statement defines a new data type, with more than one member.  The format of the struct statement is as follows
  • 6. Accessing Structure Members To access any member of a structure, we use the member access operator (.) The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type.
  • 10. Structures as Function Arguments You can pass a structure as a function argument in the same way as you pass any other variable or pointer.
  • 11. Union
  • 12. Union A union is a special data type available in C that allows to store different data types in the same memory location.  You can define a union with many members, but only one member can contain a value at any given time.  Unions provide an efficient way of using the same memory location for multiple- purpose.
  • 13. Defining a Union To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program.
  • 14. Defining a Union The format of the union statement is as follows Each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the union's definition, before the final semicolon, you can specify one or more union variables but it is optional.
  • 15. Defining a Union • Now, a variable of Data type can store an integer, a floating-point number, or a string of characters. • It means a single variable, i.e., same memory location, can be used to store multiple types of data.
  • 17. Accessing Union Members To access any member of a union, we use the member access operator (.).  The member access operator is coded as a period between the union variable name and the union member that we wish to access. You would use the keyword union to define variables of union type.
  • 20. Advantages of structure • Structures gather more than one piece of data about the same subject together in the same place. • It is helpful when you want to gather the data of similar data types and parameters like first name, last name, etc. • It is very easy to maintain as we can represent the whole record by using a single name. • In structure, we can pass complete set of records to any function using a single parameter. • You can use an array of structure to store more records with similar types.
  • 21. Disadvantag es of structure  Change of one data structure in a code necessitates changes at many other places.Therefore, the changes become hard to track.  Structure is slower because it requires storage space for all the data.  You can retrieve any member at a time in structure whereas you can access one member at a time in the union.  Structure occupies space for each and every member written in inner parameters while union occupies space for a member having the highest size written in inner parameters.  Structure supports flexible array. Union does not support a flexible array.
  • 22. Advantages of union •It occupies less memory compared to structure. •When you use union, only the last variable can be directly accessed. •Union is used when you have to use the same memory location for two or more data members. •It enables you to hold data of only one data member. •Its allocated space is equal to maximum size of the data member.
  • 23. Disadvantage s of union •You can use only one union member at a time. •All the union variables cannot be initialized or used with varying values at a time. •Union assigns one common storage space for all its members.