SlideShare a Scribd company logo
2
Most read
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.

More Related Content

PPTX
Union in c language
PPTX
Union in C programming
ODP
C union
PPT
Union In language C
PDF
Lecture19 unionsin c.ppt
DOCX
Union from C and Data Strutures
PPTX
What is Non-primitive data type?
PPTX
Edgar f
Union in c language
Union in C programming
C union
Union In language C
Lecture19 unionsin c.ppt
Union from C and Data Strutures
What is Non-primitive data type?
Edgar f

What's hot (17)

PPTX
How to create Database in Moodle
DOC
Structures unions
PPTX
Learn Database Design with MySQL - Chapter 1 - What is a database
DOC
Lab1 Assignment
DOCX
Array_Class_11_CBSE_notes
PDF
Literature Services Resource Description Framework
PDF
Chapter 4 organizing & manipulating the data in database
ODP
ppt on open office.org
PPT
Unions in c
PPT
PPS
Introduction To .Net
DOCX
PPTX
Db sys concept
PDF
All_Data_DBMS_MODELS 130725130028-phpapp01
PPTX
Difference between RDBMS & DBMS
PPTX
Database Concepts
PPTX
State of the art literature review on ...
How to create Database in Moodle
Structures unions
Learn Database Design with MySQL - Chapter 1 - What is a database
Lab1 Assignment
Array_Class_11_CBSE_notes
Literature Services Resource Description Framework
Chapter 4 organizing & manipulating the data in database
ppt on open office.org
Unions in c
Introduction To .Net
Db sys concept
All_Data_DBMS_MODELS 130725130028-phpapp01
Difference between RDBMS & DBMS
Database Concepts
State of the art literature review on ...
Ad

Similar to Union in c language (20)

PPTX
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
PPTX
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
DOCX
Union
PPTX
UNIONS IN C.pptx
PPTX
UNIONS IN C.pptx
DOCX
Structure & union
PPTX
UNION 1.pptx
PPTX
Lesson 26 c programming ( union, storage classes)
PPTX
17 structure-and-union
PPTX
Implementation of union
PPTX
Unions.pptx
PPTX
C Programming: Structure and Union
PPT
structure and union from C programming Language
PPT
structure and union from c programming language.ppt
PDF
Unit 3
PPTX
DATA TYPES IN C Language.pptx
PDF
C- language Lecture 7
PDF
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
PPTX
8.derived data types
PPTX
Programming in C
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
Union
UNIONS IN C.pptx
UNIONS IN C.pptx
Structure & union
UNION 1.pptx
Lesson 26 c programming ( union, storage classes)
17 structure-and-union
Implementation of union
Unions.pptx
C Programming: Structure and Union
structure and union from C programming Language
structure and union from c programming language.ppt
Unit 3
DATA TYPES IN C Language.pptx
C- language Lecture 7
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
8.derived data types
Programming in C
Ad

More from Tanmay Modi (12)

PPTX
Preprocessor directives in c laguage
PPTX
Pointers in c language
PPTX
Loops in c language
PPTX
Generations of computers
PPTX
Storage classes in c language
PPTX
Structures in c language
PPTX
Operators inc c language
PPTX
Functions in c language
PPTX
Dynamic memory allocation in c language
PPTX
Decision statements in c laguage
PPTX
Arrays in c v1 09102017
PPTX
Cryptocurrency
Preprocessor directives in c laguage
Pointers in c language
Loops in c language
Generations of computers
Storage classes in c language
Structures in c language
Operators inc c language
Functions in c language
Dynamic memory allocation in c language
Decision statements in c laguage
Arrays in c v1 09102017
Cryptocurrency

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Institutional Correction lecture only . . .
PDF
01-Introduction-to-Information-Management.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Classroom Observation Tools for Teachers
PDF
Computing-Curriculum for Schools in Ghana
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
master seminar digital applications in india
Pharma ospi slides which help in ospi learning
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Final Presentation General Medicine 03-08-2024.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Renaissance Architecture: A Journey from Faith to Humanism
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
TR - Agricultural Crops Production NC III.pdf
Pre independence Education in Inndia.pdf
Institutional Correction lecture only . . .
01-Introduction-to-Information-Management.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
RMMM.pdf make it easy to upload and study
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Classroom Observation Tools for Teachers
Computing-Curriculum for Schools in Ghana
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
master seminar digital applications in india

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.