SlideShare a Scribd company logo
Programming in C
UnionsUnions
BYBY
sHABEER isMAEELsHABEER isMAEEL
Unions
• AA union is a user defined data type that may holdis a user defined data type that may hold
different type of members of different sizes, BUTdifferent type of members of different sizes, BUT
only one type at a time.only one type at a time.
• All members of the union share the same
memory..
• The compiler assigns enough memory for the largestThe compiler assigns enough memory for the largest
of the member types.of the member types.
• The syntax for defining aThe syntax for defining a union and using itsand using its
members is the same as the syntax for amembers is the same as the syntax for a struct..
Formal Union Definition
• The general form of aThe general form of a unionunion definition isdefinition is
union tag
{
member1_declaration;
member2_declaration;
member3_declaration;
. . .
memberN_declaration;
};
wherewhere union is the keyword,is the keyword, tagtag names this kind ofnames this kind of union,,
andand mmember_declarations are variable declarationsare variable declarations
which define the members.which define the members.
Note that the syntax for defining aNote that the syntax for defining a union is exactly the sameis exactly the same
as the syntax for aas the syntax for a struct..
EXAMPLE OF UNION
The union of Employee is declared asThe union of Employee is declared as
union employeeunion employee
{{
int emp_id;int emp_id;
char name[20];char name[20];
float salary;float salary;
char address[50];char address[50];
int dept_no;int dept_no;
int ageint age;;
};};
MEMORY SPACE ALLOCATION
SIMPLE PROGRAMMING EXAMPLE
#include<stdio.h>#include<stdio.h>
#include<conio.h>#include<conio.h>
union employeeunion employee
{{
char name[20];char name[20];
float salary;float salary;
}u;}u;
int main()int main()
{{
clrscr();clrscr();
printf(“enter the namen”);printf(“enter the namen”);
scanf(“%s”,&u.name);scanf(“%s”,&u.name);
printf(“enter the salaryn”);printf(“enter the salaryn”);
scanf(“%f”,&u.salary);scanf(“%f”,&u.salary);
printf(“name: %sn”,u.name);printf(“name: %sn”,u.name);
printf(“salary: %f”,u.salary);printf(“salary: %f”,u.salary);
return 0;return 0;
}}
Union vs. Struct
• SimilaritiesSimilarities
– Definition syntax virtually identical
– Member access syntax identical
• DifferencesDifferences
– Members of a struct each have their own address in
memory.
– The size of a struct is at least as big as the sum of the
sizes of the members.
– Members of a union share the same memory.
– The size of a union is the size of the largest member.
Unions

More Related Content

What's hot

Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Data types
Data typesData types
Data types
Syed Umair
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
Mazharul Islam
 
Data types
Data typesData types
Data types
Nokesh Prabhakar
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
Way2itech
 
C++ string
C++ stringC++ string
C++ string
Dheenadayalan18
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
DevoAjit Gupta
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
arushi bhatnagar
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
Fazila Sadia
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data typesManisha Keim
 
Data types in C
Data types in CData types in C
Data types in C
Ansh Kashyap
 
Arrays
ArraysArrays
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
k v
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
yndaravind
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
Xml namespace
Xml namespaceXml namespace
Xml namespace
GayathriS578276
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
topu93
 
C string
C stringC string
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR
 

What's hot (20)

Strings in C
Strings in CStrings in C
Strings in C
 
Data types
Data typesData types
Data types
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Data types
Data typesData types
Data types
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
C++ string
C++ stringC++ string
C++ string
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
 
Data types in C
Data types in CData types in C
Data types in C
 
Arrays
ArraysArrays
Arrays
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Xml namespace
Xml namespaceXml namespace
Xml namespace
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
C string
C stringC string
C string
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 

Viewers also liked

structure and union
structure and unionstructure and union
structure and unionstudent
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
Rai University
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
Shareb Ismaeel
 
Union In language C
Union In language CUnion In language C
Union In language C
Ravi Singh
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
Prerna Sharma
 
What Is The Top Down Approach 2010 Pps
What Is The Top Down Approach 2010   PpsWhat Is The Top Down Approach 2010   Pps
What Is The Top Down Approach 2010 PpsSaima Khan
 
Dynamic Product Documentation
Dynamic Product DocumentationDynamic Product Documentation
Dynamic Product Documentation
Scott Abel
 
Remote mobile debugging
Remote mobile debuggingRemote mobile debugging
Remote mobile debugging
Patrick D'Souza
 
Storing Sensor data using User Defined Types
Storing Sensor data using User Defined TypesStoring Sensor data using User Defined Types
Storing Sensor data using User Defined Types
Andy Cobley
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
Dhrumil Patel
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
Kamal Acharya
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
Rai University
 
Product documentation architectural tweaks.pptx
Product documentation architectural tweaks.pptxProduct documentation architectural tweaks.pptx
Product documentation architectural tweaks.pptxGirish Mahadevan
 
Ch10
Ch10Ch10
Ch10
jashliao
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structure
Utsav276
 
03 Object Dbms Technology
03 Object Dbms Technology03 Object Dbms Technology
03 Object Dbms Technology
Laguna State Polytechnic University
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
eShikshak
 
From Top Down to Bottoms Up - The New Digital Media Approach @themediakitchen
From Top Down to Bottoms Up - The New Digital Media Approach @themediakitchenFrom Top Down to Bottoms Up - The New Digital Media Approach @themediakitchen
From Top Down to Bottoms Up - The New Digital Media Approach @themediakitchen
The Media Kitchen
 
Rookies Guide To Finding a Job By Girish Mahadevan
Rookies Guide To Finding a Job By Girish MahadevanRookies Guide To Finding a Job By Girish Mahadevan
Rookies Guide To Finding a Job By Girish Mahadevan
Girish Mahadevan
 
Approaches to problem solving and stages involved
Approaches to problem solving and stages involvedApproaches to problem solving and stages involved
Approaches to problem solving and stages involvedSounay Phothisane
 

Viewers also liked (20)

structure and union
structure and unionstructure and union
structure and union
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Union In language C
Union In language CUnion In language C
Union In language C
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 
What Is The Top Down Approach 2010 Pps
What Is The Top Down Approach 2010   PpsWhat Is The Top Down Approach 2010   Pps
What Is The Top Down Approach 2010 Pps
 
Dynamic Product Documentation
Dynamic Product DocumentationDynamic Product Documentation
Dynamic Product Documentation
 
Remote mobile debugging
Remote mobile debuggingRemote mobile debugging
Remote mobile debugging
 
Storing Sensor data using User Defined Types
Storing Sensor data using User Defined TypesStoring Sensor data using User Defined Types
Storing Sensor data using User Defined Types
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Product documentation architectural tweaks.pptx
Product documentation architectural tweaks.pptxProduct documentation architectural tweaks.pptx
Product documentation architectural tweaks.pptx
 
Ch10
Ch10Ch10
Ch10
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structure
 
03 Object Dbms Technology
03 Object Dbms Technology03 Object Dbms Technology
03 Object Dbms Technology
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
 
From Top Down to Bottoms Up - The New Digital Media Approach @themediakitchen
From Top Down to Bottoms Up - The New Digital Media Approach @themediakitchenFrom Top Down to Bottoms Up - The New Digital Media Approach @themediakitchen
From Top Down to Bottoms Up - The New Digital Media Approach @themediakitchen
 
Rookies Guide To Finding a Job By Girish Mahadevan
Rookies Guide To Finding a Job By Girish MahadevanRookies Guide To Finding a Job By Girish Mahadevan
Rookies Guide To Finding a Job By Girish Mahadevan
 
Approaches to problem solving and stages involved
Approaches to problem solving and stages involvedApproaches to problem solving and stages involved
Approaches to problem solving and stages involved
 

Similar to Unions

Unions.pptx
Unions.pptxUnions.pptx
Unions.pptx
OluwafolakeOjo
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
Ashim Lamichhane
 
Union.ppt
Union.pptUnion.ppt
Union.ppt
SureshM228
 
Structures
StructuresStructures
Structures
arshpreetkaur07
 
Session 6
Session 6Session 6
STRUCTURES_UNION.pptx
STRUCTURES_UNION.pptxSTRUCTURES_UNION.pptx
STRUCTURES_UNION.pptx
727822TUCS116MOHANKU
 
CS3251- Programming in C 1 to 5 units.pdf
CS3251- Programming in C 1 to 5 units.pdfCS3251- Programming in C 1 to 5 units.pdf
CS3251- Programming in C 1 to 5 units.pdf
priyajenat
 
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
 
Chapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptxChapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptx
Abhishekkumarsingh630054
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
nikshaikh786
 
lec14.pdf
lec14.pdflec14.pdf
lec14.pdf
HEMAHEMS5
 
SPL 14 | Structures in C
SPL 14 | Structures in CSPL 14 | Structures in C
SPL 14 | Structures in C
Mohammad Imam Hossain
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
Shankar Gangaju
 
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Rai University
 
Structure & union
Structure & unionStructure & union
Structure & union
lalithambiga kamaraj
 
Structure and union
Structure and unionStructure and union
Structure and union
Samsil Arefin
 
Structure c
Structure cStructure c
Structure c
thirumalaikumar3
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
psaravanan1985
 
Programming in C
Programming in CProgramming in C
Programming in C
MalathiNagarajan20
 

Similar to Unions (20)

Unions.pptx
Unions.pptxUnions.pptx
Unions.pptx
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Union.ppt
Union.pptUnion.ppt
Union.ppt
 
Structures
StructuresStructures
Structures
 
Session 6
Session 6Session 6
Session 6
 
STRUCTURES_UNION.pptx
STRUCTURES_UNION.pptxSTRUCTURES_UNION.pptx
STRUCTURES_UNION.pptx
 
CS3251- Programming in C 1 to 5 units.pdf
CS3251- Programming in C 1 to 5 units.pdfCS3251- Programming in C 1 to 5 units.pdf
CS3251- Programming in C 1 to 5 units.pdf
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
Chapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptxChapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptx
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
lec14.pdf
lec14.pdflec14.pdf
lec14.pdf
 
SPL 14 | Structures in C
SPL 14 | Structures in CSPL 14 | Structures in C
SPL 14 | Structures in C
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
 
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Structure and union
Structure and unionStructure and union
Structure and union
 
Structure c
Structure cStructure c
Structure c
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Programming in C
Programming in CProgramming in C
Programming in C
 

More from Shareb Ismaeel

Cybercrimes
CybercrimesCybercrimes
Cybercrimes
Shareb Ismaeel
 
Multiprocessor structures
Multiprocessor structuresMultiprocessor structures
Multiprocessor structures
Shareb Ismaeel
 
Installation testing
Installation testingInstallation testing
Installation testing
Shareb Ismaeel
 
Genome data management
Genome data managementGenome data management
Genome data management
Shareb Ismaeel
 
E mail systems
E mail systemsE mail systems
E mail systems
Shareb Ismaeel
 
E commerce
E commerceE commerce
E commerce
Shareb Ismaeel
 
Disk structure
Disk structureDisk structure
Disk structure
Shareb Ismaeel
 
.Netframework
.Netframework.Netframework
.Netframework
Shareb Ismaeel
 

More from Shareb Ismaeel (8)

Cybercrimes
CybercrimesCybercrimes
Cybercrimes
 
Multiprocessor structures
Multiprocessor structuresMultiprocessor structures
Multiprocessor structures
 
Installation testing
Installation testingInstallation testing
Installation testing
 
Genome data management
Genome data managementGenome data management
Genome data management
 
E mail systems
E mail systemsE mail systems
E mail systems
 
E commerce
E commerceE commerce
E commerce
 
Disk structure
Disk structureDisk structure
Disk structure
 
.Netframework
.Netframework.Netframework
.Netframework
 

Recently uploaded

LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 

Recently uploaded (20)

LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 

Unions

  • 2. Unions • AA union is a user defined data type that may holdis a user defined data type that may hold different type of members of different sizes, BUTdifferent type of members of different sizes, BUT only one type at a time.only one type at a time. • All members of the union share the same memory.. • The compiler assigns enough memory for the largestThe compiler assigns enough memory for the largest of the member types.of the member types. • The syntax for defining aThe syntax for defining a union and using itsand using its members is the same as the syntax for amembers is the same as the syntax for a struct..
  • 3. Formal Union Definition • The general form of aThe general form of a unionunion definition isdefinition is union tag { member1_declaration; member2_declaration; member3_declaration; . . . memberN_declaration; }; wherewhere union is the keyword,is the keyword, tagtag names this kind ofnames this kind of union,, andand mmember_declarations are variable declarationsare variable declarations which define the members.which define the members. Note that the syntax for defining aNote that the syntax for defining a union is exactly the sameis exactly the same as the syntax for aas the syntax for a struct..
  • 4. EXAMPLE OF UNION The union of Employee is declared asThe union of Employee is declared as union employeeunion employee {{ int emp_id;int emp_id; char name[20];char name[20]; float salary;float salary; char address[50];char address[50]; int dept_no;int dept_no; int ageint age;; };};
  • 6. SIMPLE PROGRAMMING EXAMPLE #include<stdio.h>#include<stdio.h> #include<conio.h>#include<conio.h> union employeeunion employee {{ char name[20];char name[20]; float salary;float salary; }u;}u; int main()int main() {{ clrscr();clrscr(); printf(“enter the namen”);printf(“enter the namen”); scanf(“%s”,&u.name);scanf(“%s”,&u.name); printf(“enter the salaryn”);printf(“enter the salaryn”); scanf(“%f”,&u.salary);scanf(“%f”,&u.salary); printf(“name: %sn”,u.name);printf(“name: %sn”,u.name); printf(“salary: %f”,u.salary);printf(“salary: %f”,u.salary); return 0;return 0; }}
  • 7. Union vs. Struct • SimilaritiesSimilarities – Definition syntax virtually identical – Member access syntax identical • DifferencesDifferences – Members of a struct each have their own address in memory. – The size of a struct is at least as big as the sum of the sizes of the members. – Members of a union share the same memory. – The size of a union is the size of the largest member.