SlideShare a Scribd company logo
Union in C
Definition
 Unions are conceptually similar
to structures.
 The syntax of union is also similar to
that of structure.
 The only differences is in terms of
storage.
 In structure each member has its own
storage location, whereas all members
of union uses a single shared memory
location which is equal to the size of its
largest data member.
Union in c language
 This implies that although a union may
contain many members of different types, it
cannot handle all the members at same
time. A union is declared
using union keyword.
 This declares a variable It1 of type union item.
This union contains three members each with a
different data type. However only one of them can be
used at a time. This is due to the fact that only one
location is allocated for a union variable, irrespective of
its size. The compiler allocates the storage that is large
enough to hold largest variable type in the union. In
the union declared above the member x requires 4
bytes which is largest among the members in 16-bit
Accessing a Union Member
 Syntax for accessing union member
is similar to accessing structure
member,
Complete Example for Union
here, the values
of a and b get
corrupted and only
variable c prints the
expected result. This
is because in union,
the memory is
shared among
different data types.
Hence, the only
member whose
value is currently
stored will have the
memory.
Ad

More Related Content

What's hot (17)

How to create Database in Moodle
How to create  Database in MoodleHow to create  Database in Moodle
How to create Database in Moodle
Yulia Ivanova
 
Structures unions
Structures  unionsStructures  unions
Structures unions
Sugnan M
 
Learn Database Design with MySQL - Chapter 1 - What is a database
Learn Database Design with MySQL - Chapter 1 -   What is a databaseLearn Database Design with MySQL - Chapter 1 -   What is a database
Learn Database Design with MySQL - Chapter 1 - What is a database
Eduonix Learning Solutions
 
Lab1 Assignment
Lab1 AssignmentLab1 Assignment
Lab1 Assignment
cportin
 
Array_Class_11_CBSE_notes
Array_Class_11_CBSE_notesArray_Class_11_CBSE_notes
Array_Class_11_CBSE_notes
Krishnaprasad Arumugam
 
Literature Services Resource Description Framework
Literature Services Resource Description FrameworkLiterature Services Resource Description Framework
Literature Services Resource Description Framework
Jee-Hyub Kim
 
Chapter 4 organizing & manipulating the data in database
Chapter 4 organizing & manipulating the data in databaseChapter 4 organizing & manipulating the data in database
Chapter 4 organizing & manipulating the data in database
JewelMaeSantos
 
ppt on open office.org
ppt on open office.orgppt on open office.org
ppt on open office.org
Deepansh Goel
 
Unions in c
Unions in cUnions in c
Unions in c
Prabhu Govind
 
Dbms1
Dbms1Dbms1
Dbms1
Sabeeh Ahmed
 
Introduction To .Net
Introduction To .NetIntroduction To .Net
Introduction To .Net
Soumya Ramachandran
 
NoSQL
NoSQLNoSQL
NoSQL
Mian Basam Ahmad
 
Db sys concept
Db sys conceptDb sys concept
Db sys concept
Mr Patrick NIYISHAKA
 
All_Data_DBMS_MODELS 130725130028-phpapp01
All_Data_DBMS_MODELS 130725130028-phpapp01All_Data_DBMS_MODELS 130725130028-phpapp01
All_Data_DBMS_MODELS 130725130028-phpapp01
RollyTripathi
 
Difference between RDBMS & DBMS
Difference between RDBMS & DBMSDifference between RDBMS & DBMS
Difference between RDBMS & DBMS
Risha Bagchi
 
Database Concepts
Database ConceptsDatabase Concepts
Database Concepts
Saradha Shyam
 
State of the art literature review on ...
State of the art literature review on ...State of the art literature review on ...
State of the art literature review on ...
dutra2009
 
How to create Database in Moodle
How to create  Database in MoodleHow to create  Database in Moodle
How to create Database in Moodle
Yulia Ivanova
 
Structures unions
Structures  unionsStructures  unions
Structures unions
Sugnan M
 
Learn Database Design with MySQL - Chapter 1 - What is a database
Learn Database Design with MySQL - Chapter 1 -   What is a databaseLearn Database Design with MySQL - Chapter 1 -   What is a database
Learn Database Design with MySQL - Chapter 1 - What is a database
Eduonix Learning Solutions
 
Lab1 Assignment
Lab1 AssignmentLab1 Assignment
Lab1 Assignment
cportin
 
Literature Services Resource Description Framework
Literature Services Resource Description FrameworkLiterature Services Resource Description Framework
Literature Services Resource Description Framework
Jee-Hyub Kim
 
Chapter 4 organizing & manipulating the data in database
Chapter 4 organizing & manipulating the data in databaseChapter 4 organizing & manipulating the data in database
Chapter 4 organizing & manipulating the data in database
JewelMaeSantos
 
ppt on open office.org
ppt on open office.orgppt on open office.org
ppt on open office.org
Deepansh Goel
 
All_Data_DBMS_MODELS 130725130028-phpapp01
All_Data_DBMS_MODELS 130725130028-phpapp01All_Data_DBMS_MODELS 130725130028-phpapp01
All_Data_DBMS_MODELS 130725130028-phpapp01
RollyTripathi
 
Difference between RDBMS & DBMS
Difference between RDBMS & DBMSDifference between RDBMS & DBMS
Difference between RDBMS & DBMS
Risha Bagchi
 
State of the art literature review on ...
State of the art literature review on ...State of the art literature review on ...
State of the art literature review on ...
dutra2009
 

Similar to Union in c language (20)

Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptxUnit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
vekariyakashyap
 
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptxUnit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
GImpact
 
Union
UnionUnion
Union
Acad
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
SKUP1
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
LECO9
 
Structure & union
Structure & unionStructure & union
Structure & union
Vidyasagar University
 
UNION 1.pptx
UNION 1.pptxUNION 1.pptx
UNION 1.pptx
FarhanAbdullah29
 
Lesson 26 c programming ( union, storage classes)
Lesson 26 c programming ( union, storage classes)Lesson 26 c programming ( union, storage classes)
Lesson 26 c programming ( union, storage classes)
AparnaPriyadarsiniMe
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
Rohit Shrivastava
 
Implementation of union
Implementation of unionImplementation of union
Implementation of union
rajshreemuthiah
 
Unions.pptx
Unions.pptxUnions.pptx
Unions.pptx
OluwafolakeOjo
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
Selvaraj Seerangan
 
structure and union from C programming Language
structure and union from C programming Languagestructure and union from C programming Language
structure and union from C programming Language
AvrajeetGhosh
 
structure and union from c programming language.ppt
structure and union from c programming language.pptstructure and union from c programming language.ppt
structure and union from c programming language.ppt
AvrajeetGhosh
 
Unit 3
Unit 3Unit 3
Unit 3
TPLatchoumi
 
DATA TYPES IN C Language.pptx
DATA TYPES IN C Language.pptxDATA TYPES IN C Language.pptx
DATA TYPES IN C Language.pptx
karansethi63
 
C- language Lecture 7
C- language Lecture 7C- language Lecture 7
C- language Lecture 7
Hatem Abd El-Salam
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
8.derived data types
8.derived data types8.derived data types
8.derived data types
Hardik gupta
 
Programming in C
Programming in CProgramming in C
Programming in C
MalathiNagarajan20
 
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptxUnit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
vekariyakashyap
 
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptxUnit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
GImpact
 
Union
UnionUnion
Union
Acad
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
SKUP1
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
LECO9
 
Lesson 26 c programming ( union, storage classes)
Lesson 26 c programming ( union, storage classes)Lesson 26 c programming ( union, storage classes)
Lesson 26 c programming ( union, storage classes)
AparnaPriyadarsiniMe
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
Selvaraj Seerangan
 
structure and union from C programming Language
structure and union from C programming Languagestructure and union from C programming Language
structure and union from C programming Language
AvrajeetGhosh
 
structure and union from c programming language.ppt
structure and union from c programming language.pptstructure and union from c programming language.ppt
structure and union from c programming language.ppt
AvrajeetGhosh
 
DATA TYPES IN C Language.pptx
DATA TYPES IN C Language.pptxDATA TYPES IN C Language.pptx
DATA TYPES IN C Language.pptx
karansethi63
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
8.derived data types
8.derived data types8.derived data types
8.derived data types
Hardik gupta
 
Ad

More from Tanmay Modi (12)

Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
Tanmay Modi
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
Tanmay Modi
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
Tanmay Modi
 
Generations of computers
Generations of computersGenerations of computers
Generations of computers
Tanmay Modi
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
Tanmay Modi
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
Tanmay Modi
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
Tanmay Modi
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c language
Tanmay Modi
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
Tanmay Modi
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
Tanmay Modi
 
Arrays in c v1 09102017
Arrays in c v1 09102017Arrays in c v1 09102017
Arrays in c v1 09102017
Tanmay Modi
 
Cryptocurrency
Cryptocurrency Cryptocurrency
Cryptocurrency
Tanmay Modi
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
Tanmay Modi
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
Tanmay Modi
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
Tanmay Modi
 
Generations of computers
Generations of computersGenerations of computers
Generations of computers
Tanmay Modi
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
Tanmay Modi
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
Tanmay Modi
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
Tanmay Modi
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c language
Tanmay Modi
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
Tanmay Modi
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
Tanmay Modi
 
Arrays in c v1 09102017
Arrays in c v1 09102017Arrays in c v1 09102017
Arrays in c v1 09102017
Tanmay Modi
 
Ad

Recently uploaded (20)

Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 

Union in c language

  • 2. Definition  Unions are conceptually similar to structures.  The syntax of union is also similar to that of structure.  The only differences is in terms of storage.  In structure each member has its own storage location, whereas all members of union uses a single shared memory location which is equal to the size of its largest data member.
  • 4.  This implies that although a union may contain many members of different types, it cannot handle all the members at same time. A union is declared using union keyword.  This declares a variable It1 of type union item. This union contains three members each with a different data type. However only one of them can be used at a time. This is due to the fact that only one location is allocated for a union variable, irrespective of its size. The compiler allocates the storage that is large enough to hold largest variable type in the union. In the union declared above the member x requires 4 bytes which is largest among the members in 16-bit
  • 5. Accessing a Union Member  Syntax for accessing union member is similar to accessing structure member,
  • 6. Complete Example for Union here, the values of a and b get corrupted and only variable c prints the expected result. This is because in union, the memory is shared among different data types. Hence, the only member whose value is currently stored will have the memory.