SlideShare a Scribd company logo
1 of 5
Union
• Unions are similar to structure. Its syntax and use is similar to
structure.
• The distinction is that all members within union share the same
storage area of computer memory, whereas each member within a
structure is assigned its own unique storage.
• Thus unions are used to conserve memory. Since same memory is
shared by all members, one variable can reside into memory at a
time.
• When another variable is set into memory, the previous is replaced
i.e. previous can not persist.
• Thus unions are useful for applications involving multiple members
where values need to be assigned to members at the same time.
• Therefore, although a union may contain many members of different
types, it can handle only one member at a time.
• The compiler allocates a piece of storage that is large enough to hold
largest variable type in the union.
• For example:
union student{
int roll;
float marks;
};
• Here, the union student has members roll and marks.
• Roll requires 2 bytes in memory and the marks contains 4 bytes in
memory.
• As all union members share same memory, the compiles allocates
large memory (i.e. 4 bytes in this case).
• The declaration of union and its variable is similar to that of structure.
Example
#include <stdio.h>
int main(){
union students{
int roll;
float marks;
}
union student st;
st.roll=455;
printf(“Roll=%dn”, st.roll);
st.marks=67;
printf(“Marks=%fn”, st.marks);
}
Output:
Roll=455
Marks=67
#include <stdio.h>
int main(){
union students{
int roll;
float marks;
}
union student st;
st.roll=455;
st.marks=67;
printf(“Roll=%dn”, st.roll);
printf(“Marks=%fn”, st.marks);
}
Output:
Roll=0
Marks=67
Here roll is zero as
memory is replaced by
another variable st.marks
Difference between Structure and Union
Structure Union
1. Each member within a structure is assigned its
own unique storage. It takes more memory than
union.
All members within union share the same storage
area of computer memory. It takes less memory than
structure.
2. The amount of memory required to store a
structure is the sum of the sizes of all members.
The amount of memory required to store an union is
same as member that occupies largest memory.
3. All the structure members can be accessed at any
point of time.
Only one member of union can be accessed at any
given time.
4. Structure is declared as:
struct student{
int roll;
float marks;
}st;
Union is declared as:
union student{
int roll;
float marks;
}st;

More Related Content

Similar to UNION 1.pptx

Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
DigitalDsms
 

Similar to UNION 1.pptx (20)

Union
UnionUnion
Union
 
Union in c language
Union in c languageUnion in c language
Union in c language
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Unions.pptx
Unions.pptxUnions.pptx
Unions.pptx
 
C- language Lecture 7
C- language Lecture 7C- language Lecture 7
C- language Lecture 7
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
Union from C and Data Strutures
Union from C and Data StruturesUnion from C and Data Strutures
Union from C and Data Strutures
 
1. structure
1. structure1. structure
1. structure
 
Structure in C
Structure in CStructure in C
Structure in C
 
Union
UnionUnion
Union
 
C Programming - Refresher - Part IV
C Programming - Refresher - Part IVC Programming - Refresher - Part IV
C Programming - Refresher - Part IV
 
C# structure
C# structureC# structure
C# structure
 
Stack n heap
Stack n heapStack n heap
Stack n heap
 
Constant a,variables and data types
Constant a,variables and data typesConstant a,variables and data types
Constant a,variables and data types
 
Stack&heap
Stack&heapStack&heap
Stack&heap
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
 
STORAGE CLASS.pptx
STORAGE CLASS.pptxSTORAGE CLASS.pptx
STORAGE CLASS.pptx
 
Session 6
Session 6Session 6
Session 6
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
C programming session7
C programming  session7C programming  session7
C programming session7
 

Recently uploaded

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 

Recently uploaded (20)

Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 

UNION 1.pptx

  • 1. Union • Unions are similar to structure. Its syntax and use is similar to structure. • The distinction is that all members within union share the same storage area of computer memory, whereas each member within a structure is assigned its own unique storage. • Thus unions are used to conserve memory. Since same memory is shared by all members, one variable can reside into memory at a time. • When another variable is set into memory, the previous is replaced i.e. previous can not persist.
  • 2. • Thus unions are useful for applications involving multiple members where values need to be assigned to members at the same time. • Therefore, although a union may contain many members of different types, it can handle only one member at a time. • The compiler allocates a piece of storage that is large enough to hold largest variable type in the union. • For example: union student{ int roll; float marks; };
  • 3. • Here, the union student has members roll and marks. • Roll requires 2 bytes in memory and the marks contains 4 bytes in memory. • As all union members share same memory, the compiles allocates large memory (i.e. 4 bytes in this case). • The declaration of union and its variable is similar to that of structure.
  • 4. Example #include <stdio.h> int main(){ union students{ int roll; float marks; } union student st; st.roll=455; printf(“Roll=%dn”, st.roll); st.marks=67; printf(“Marks=%fn”, st.marks); } Output: Roll=455 Marks=67 #include <stdio.h> int main(){ union students{ int roll; float marks; } union student st; st.roll=455; st.marks=67; printf(“Roll=%dn”, st.roll); printf(“Marks=%fn”, st.marks); } Output: Roll=0 Marks=67 Here roll is zero as memory is replaced by another variable st.marks
  • 5. Difference between Structure and Union Structure Union 1. Each member within a structure is assigned its own unique storage. It takes more memory than union. All members within union share the same storage area of computer memory. It takes less memory than structure. 2. The amount of memory required to store a structure is the sum of the sizes of all members. The amount of memory required to store an union is same as member that occupies largest memory. 3. All the structure members can be accessed at any point of time. Only one member of union can be accessed at any given time. 4. Structure is declared as: struct student{ int roll; float marks; }st; Union is declared as: union student{ int roll; float marks; }st;