SlideShare a Scribd company logo
1 of 8
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 (20)

Strings in c language
Strings in  c languageStrings in  c language
Strings in c language
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Input And Output
 Input And Output Input And Output
Input And Output
 
Typedef
TypedefTypedef
Typedef
 
pointers
pointerspointers
pointers
 
Function in c
Function in cFunction in c
Function in c
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Function in C
Function in CFunction in C
Function in C
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and Typedef
 
Control statements
Control statementsControl statements
Control statements
 
String In C Language
String In C Language String In C Language
String In C Language
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
C string
C stringC string
C string
 
C pointer
C pointerC pointer
C pointer
 
Branching statements
Branching statementsBranching statements
Branching statements
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
 

Viewers also liked

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 handlingRai University
 
Union In language C
Union In language CUnion In language C
Union In language CRavi Singh
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3Prerna 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 DocumentationScott Abel
 
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 TypesAndy Cobley
 
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 handlingRai University
 
Product documentation architectural tweaks.pptx
Product documentation architectural tweaks.pptxProduct documentation architectural tweaks.pptx
Product documentation architectural tweaks.pptxGirish Mahadevan
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structureUtsav276
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppteShikshak
 
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 @themediakitchenThe 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 MahadevanGirish 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
 
Strategies for Debugging Print CSS
Strategies for Debugging Print CSSStrategies for Debugging Print CSS
Strategies for Debugging Print CSSMrDys
 
Tools and Techniques for Faster Development
Tools and Techniques for Faster DevelopmentTools and Techniques for Faster Development
Tools and Techniques for Faster Developmentjtaby
 

Viewers also liked (20)

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
 
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
 
Strategies for Debugging Print CSS
Strategies for Debugging Print CSSStrategies for Debugging Print CSS
Strategies for Debugging Print CSS
 
Tools and Techniques for Faster Development
Tools and Techniques for Faster DevelopmentTools and Techniques for Faster Development
Tools and Techniques for Faster Development
 

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 and union
Structure and unionStructure and union
Structure and union
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Unit 5 structure and unions
Unit 5 structure and unionsUnit 5 structure and unions
Unit 5 structure and unions
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 

More from 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

Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
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 PPTbhaskargani46
 
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.pptNANDHAKUMARA10
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
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.pdfJiananWang21
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086anil_gaur
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
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 ptxJIT KUMAR GUPTA
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 

Recently uploaded (20)

Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
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
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
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
 
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
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
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
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
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
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 

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.