SlideShare a Scribd company logo
1 of 5
Union
• A union is declared using the keyword union as:
union student
{
char name[20];
int roll_no;
float marks;
char section;
};
union student s;
• While accessing union members, we should make sure that
we are accessing the member whose value is currently
residing in the memory. Otherwise we will get erroneous
output (which is machine dependent).
2
Unions:
• A union is a variable that may hold (at different
times) objects of different types and sizes, with the
compiler keeping track of size and alignment
requirements.
• Unions provide a way to manipulate different kinds
of data in a single area of storage, without
embedding any machine-dependent information in
the program.
• Both structure and unions are used to group a
number of different variables together.
Syntactically both structure and unions are exactly
same. The main difference between them is in
storage.
• In structures, each member has its own memory
location but all members of union use the same
memory location which is equal to the greatest
member’s size.
3
Example: program illustrates the structure in which read member elements of union and display
them.
#include<stdio.h>
union Student
{
char name[20];
int roll;
char sec;
F loat marks;
};
union Student s1;
void main()
{
printf(“Enter the name of a student”);
gets(s1.name);
printf(“Enter the roll number of a student”);
scanf(“%d”,&s1.roll);
printf(“Enter the section of a student”);
scanf(“%c”,&s1.sec);
printf(“Enter the marks obtained by the student”);
scanf(“%f”,&s1.marks);
/displaying the records
printf(“Name=%sn Roll number =%dn Section=%cn Obtained marks=%f”,s1.name,
s1.roll, s1.sec, s1.marks);
}
4
Example:
union student
{
char name[20];
int roll_no;
float marks;
char section;
};
void main()
{
union student s;
clrscr();
printf("Size of union=%dn", sizeof(s));
strcpy(s.name, “cnr");
printf("Name=%st", s.name);
printf("Location=%u", s.name);
s.roll_no=10;
printf("nRoll=%d tLocation=%u", s.roll_no, &s.roll_no);
s.marks=91.5;
printf("nMarks=%.2f tLocation=%u", s.marks, &s.marks);
s.section='A';
printf("nSection=%c tLocation=%u", s.section, &s.section);
printf("nnErroneous output");
printf("nnNametRolltMarkstSectionn");
printf("%st%dt%.2ft%c", s.name, s.roll_no, s.marks, s.section);
getch();
}
5

More Related Content

What's hot

Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C ProgrammingDevoAjit Gupta
 
Structure in c language
Structure in c languageStructure in c language
Structure in c languagesangrampatil81
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures topu93
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
structure and union
structure and unionstructure and union
structure and unionstudent
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array pptsandhya yadav
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in CSahithi Naraparaju
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Functions in python
Functions in pythonFunctions in python
Functions in pythoncolorsof
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programmingprogramming9
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - PointersWingston
 

What's hot (20)

Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Strings
StringsStrings
Strings
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
structure and union
structure and unionstructure and union
structure and union
 
Arrays
ArraysArrays
Arrays
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
C pointer
C pointerC pointer
C pointer
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 

Viewers also liked

Viewers also liked (20)

Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Strings in C
Strings in CStrings in C
Strings in C
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
Recursion transformer
Recursion transformerRecursion transformer
Recursion transformer
 
cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basics
 
Input output streams
Input output streamsInput output streams
Input output streams
 
C tutorial
C tutorialC tutorial
C tutorial
 
Secondary storage structure
Secondary storage structureSecondary storage structure
Secondary storage structure
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
The Algebra of Functions
The Algebra of FunctionsThe Algebra of Functions
The Algebra of Functions
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
C string
C stringC string
C string
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Structure in C
Structure in CStructure in C
Structure in C
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Secondary storage structure-Operating System Concepts
Secondary storage structure-Operating System ConceptsSecondary storage structure-Operating System Concepts
Secondary storage structure-Operating System Concepts
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 

Similar to Union in C programming

Union from C and Data Strutures
Union from C and Data StruturesUnion from C and Data Strutures
Union from C and Data StruturesAcad
 
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.pptxvekariyakashyap
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptxSKUP1
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptxLECO9
 
Union
UnionUnion
UnionAcad
 
Union in c language
Union in c languageUnion in c language
Union in c languageTanmay Modi
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and UnionsAshim Lamichhane
 
Structures unions
Structures  unionsStructures  unions
Structures unionsSugnan M
 
Useful and Effectiveness of Multi Agent System
Useful and Effectiveness of Multi Agent SystemUseful and Effectiveness of Multi Agent System
Useful and Effectiveness of Multi Agent Systemijtsrd
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and UnionSelvaraj Seerangan
 

Similar to Union in C programming (20)

Union from C and Data Strutures
Union from C and Data StruturesUnion from C and Data Strutures
Union from C and Data Strutures
 
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
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
UNION 1.pptx
UNION 1.pptxUNION 1.pptx
UNION 1.pptx
 
Union
UnionUnion
Union
 
Union in c language
Union in c languageUnion in c language
Union in c language
 
Unions.pptx
Unions.pptxUnions.pptx
Unions.pptx
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
C- language Lecture 7
C- language Lecture 7C- language Lecture 7
C- language Lecture 7
 
Union.ppt
Union.pptUnion.ppt
Union.ppt
 
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
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
C union
C unionC union
C union
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Structures unions
Structures  unionsStructures  unions
Structures unions
 
Useful and Effectiveness of Multi Agent System
Useful and Effectiveness of Multi Agent SystemUseful and Effectiveness of Multi Agent System
Useful and Effectiveness of Multi Agent System
 
Operating systems1[1]
Operating systems1[1]Operating systems1[1]
Operating systems1[1]
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 

More from Kamal Acharya

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computerKamal Acharya
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer SecurityKamal Acharya
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHPKamal Acharya
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in phpKamal Acharya
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPKamal Acharya
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data WarehousingKamal Acharya
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data MiningKamal Acharya
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data MiningKamal Acharya
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data miningKamal Acharya
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingKamal Acharya
 

More from Kamal Acharya (20)

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 
Computer Arithmetic
Computer ArithmeticComputer Arithmetic
Computer Arithmetic
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer Security
 
Session and Cookies
Session and CookiesSession and Cookies
Session and Cookies
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
Web forms in php
Web forms in phpWeb forms in php
Web forms in php
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHP
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in php
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data Warehousing
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Search Engines
Search EnginesSearch Engines
Search Engines
 
Web Mining
Web MiningWeb Mining
Web Mining
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data Mining
 
Cluster Analysis
Cluster AnalysisCluster Analysis
Cluster Analysis
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data Mining
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data mining
 
Data Preprocessing
Data PreprocessingData Preprocessing
Data Preprocessing
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data Warehousing
 

Recently uploaded

General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

Union in C programming

  • 2. • A union is declared using the keyword union as: union student { char name[20]; int roll_no; float marks; char section; }; union student s; • While accessing union members, we should make sure that we are accessing the member whose value is currently residing in the memory. Otherwise we will get erroneous output (which is machine dependent). 2
  • 3. Unions: • A union is a variable that may hold (at different times) objects of different types and sizes, with the compiler keeping track of size and alignment requirements. • Unions provide a way to manipulate different kinds of data in a single area of storage, without embedding any machine-dependent information in the program. • Both structure and unions are used to group a number of different variables together. Syntactically both structure and unions are exactly same. The main difference between them is in storage. • In structures, each member has its own memory location but all members of union use the same memory location which is equal to the greatest member’s size. 3
  • 4. Example: program illustrates the structure in which read member elements of union and display them. #include<stdio.h> union Student { char name[20]; int roll; char sec; F loat marks; }; union Student s1; void main() { printf(“Enter the name of a student”); gets(s1.name); printf(“Enter the roll number of a student”); scanf(“%d”,&s1.roll); printf(“Enter the section of a student”); scanf(“%c”,&s1.sec); printf(“Enter the marks obtained by the student”); scanf(“%f”,&s1.marks); /displaying the records printf(“Name=%sn Roll number =%dn Section=%cn Obtained marks=%f”,s1.name, s1.roll, s1.sec, s1.marks); } 4
  • 5. Example: union student { char name[20]; int roll_no; float marks; char section; }; void main() { union student s; clrscr(); printf("Size of union=%dn", sizeof(s)); strcpy(s.name, “cnr"); printf("Name=%st", s.name); printf("Location=%u", s.name); s.roll_no=10; printf("nRoll=%d tLocation=%u", s.roll_no, &s.roll_no); s.marks=91.5; printf("nMarks=%.2f tLocation=%u", s.marks, &s.marks); s.section='A'; printf("nSection=%c tLocation=%u", s.section, &s.section); printf("nnErroneous output"); printf("nnNametRolltMarkstSectionn"); printf("%st%dt%.2ft%c", s.name, s.roll_no, s.marks, s.section); getch(); } 5