SlideShare a Scribd company logo
1 of 14
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
Session Objectives
Discuss Unions
Create Union variable
Access Union member using . operator
Discuss Unions of Structures
Compare Structure and Array
Difference between structure & union
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
Unions are collection of different datatype
grouped together under a single variable name for
convienient handling.
syntax : union <union name >
{
datatype member1;
datatype member2;
};
union book
{
int pages;
char bookname[10];
char author[20];
float price;
};
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
For Example :
#include<stdio.h>
union amount
{
int ac_no;
char name[10];
char city [20];
int bal;
}a;
void main()
{
printf("Enter the account Details:n");
scanf("%d%s%d",&a.ac_no, &a.name, &a.state, &a.bal);
printf(“%dn%sn%dn", a.ac_no, a.name, a.city, a.bal);
printf("%dn",sizeof(union amount));
getch();
}
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
/* For Example 2 */
#include<stdio.h>
#include<conio.h>
void main()
{
union a
{
int i;
char ch[2];
}a1;
a1.i=150;
clrscr();
printf("%d %c
%c",a1.i,a1.ch[0],a1.ch[1]);
getch();
}
OUTPUT :
150 ã
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
Example 3 :
#include<stdio.h>
union amount
{
int acct_no;
int acct_type;
char name[10];
char street[50];
char city_state[20];
int balance;
}a;
void main()
{
printf("Enter the account Details:n");
scanf("%d%d%s%s%d"
,&a.acct_no,&a.acct_type,&a.name,&a.city_state,&a.balance);
printf("The values are %dn%dn%sn%sn%dn", a.acct_no, a.acct_type,
a.name, a.city_state, a.balance);*/
printf("%dn",sizeof(union amount));
}
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
/*Sizeof Union*/
#include<stdio.h>
#include<conio.h>
union example
{
int sno;
char sname[20];
int marks;
}e;
main()
{
clrscr();
printf("n Sizeof sno:%d",sizeof(e.sno));
printf("n Sizeof sname:%d",sizeof(e.sname));
printf("n Sizeof marks:%d",sizeof(e.marks));
printf("n Sizeof Union is:%d",sizeof(e));
getch();
}
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
Unions of Structures
It is possible to nest unions within unions, unions in
structures,
and structures in unions.
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
Structure Arrays
It is an single entity representing a
Collection of data types of different
data types
It is an single entity representing a
Collection of data types of same data
types
Individual entries in a structure are
called Members
Individual entries in a array are called
array elements
The members of a structure are not
stored in sequence of memory
locations
The members of a array are stored in
sequence of memory locations
All the members of a structure can be
initialized
Only the first member of an union can be
initialized
Individual structure elements are
referred through the use of .(dot or
membership) operator
Individual array elements are accessed by
its name followed by the square braces[]
within which the index is placed
Initialization of elements can be done
only during structure definition
Initialization of elements can be done
during array declaration
Structure definition reserve enough
memory space for its members
Array declaration reserve enough memory
space for its elements
The keyword ‘struct’ tells us what we
are dealing with structure
The is no keyword to represent arrays
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
Structure Union
Keyword : struct Keyword : union
Each member in a structure occupies
and uses its own memory space
All the members of a union use the same
memory space
More memory space is required since
each member is stored in a separate
memory locations
Less memory space is required since all
member is stored in the same memory
locations
All the members of a structure can be
initialized
Only the first member of an union can be
initialized
The variables that make up the
structure are called Structure
variable
The variables that make up the union are
called union variable
Individual structure elements are
referred through the use of .(dot or
membership) operator
Individual union elements are referred
through the use of .(dot or membership )
operator
Structure is a user-defined data type.
Union is a user-defined data type.
Possible to have one structure within
another structure
Unions within union is possible
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
Session Summary
 The union is another compound data type may hold the variables of different types and
sizes with the compiler keeping track of the size.
The memory space reserved for a union members is large enough to store its largest
member
 The keyword “union” begins the union declaration followed by the tag (i.e., union
name)
 Within the braces union members are declared
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
EXERCISES
1. Define a union called student that will describe the following information student
name,class,rollno, subject marks & total. Using student declare an array stu_list
with 30 elements. Write program in C to read the information about all the 30
students and to display the information?

More Related Content

Similar to Union.ppt

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 UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
Rajeshkumar Reddy
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 

Similar to Union.ppt (20)

Structures
StructuresStructures
Structures
 
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 UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
 
Structure In C
Structure In CStructure In C
Structure In C
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Unit 3
Unit 3Unit 3
Unit 3
 
CP Handout#10
CP Handout#10CP Handout#10
CP Handout#10
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Structure.pptx
Structure.pptxStructure.pptx
Structure.pptx
 
Unions.pptx
Unions.pptxUnions.pptx
Unions.pptx
 
Unions
UnionsUnions
Unions
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
Structures
StructuresStructures
Structures
 
Structure c
Structure cStructure c
Structure c
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 

Union.ppt

  • 2. CSC COMPUTER EDUCATION, M.K.B.NAGAR Session Objectives Discuss Unions Create Union variable Access Union member using . operator Discuss Unions of Structures Compare Structure and Array Difference between structure & union
  • 3. CSC COMPUTER EDUCATION, M.K.B.NAGAR Unions are collection of different datatype grouped together under a single variable name for convienient handling. syntax : union <union name > { datatype member1; datatype member2; }; union book { int pages; char bookname[10]; char author[20]; float price; };
  • 4. CSC COMPUTER EDUCATION, M.K.B.NAGAR For Example : #include<stdio.h> union amount { int ac_no; char name[10]; char city [20]; int bal; }a; void main() { printf("Enter the account Details:n"); scanf("%d%s%d",&a.ac_no, &a.name, &a.state, &a.bal); printf(“%dn%sn%dn", a.ac_no, a.name, a.city, a.bal); printf("%dn",sizeof(union amount)); getch(); }
  • 5. CSC COMPUTER EDUCATION, M.K.B.NAGAR /* For Example 2 */ #include<stdio.h> #include<conio.h> void main() { union a { int i; char ch[2]; }a1; a1.i=150; clrscr(); printf("%d %c %c",a1.i,a1.ch[0],a1.ch[1]); getch(); } OUTPUT : 150 ã
  • 6. CSC COMPUTER EDUCATION, M.K.B.NAGAR Example 3 : #include<stdio.h> union amount { int acct_no; int acct_type; char name[10]; char street[50]; char city_state[20]; int balance; }a; void main() { printf("Enter the account Details:n"); scanf("%d%d%s%s%d" ,&a.acct_no,&a.acct_type,&a.name,&a.city_state,&a.balance); printf("The values are %dn%dn%sn%sn%dn", a.acct_no, a.acct_type, a.name, a.city_state, a.balance);*/ printf("%dn",sizeof(union amount)); }
  • 7. CSC COMPUTER EDUCATION, M.K.B.NAGAR /*Sizeof Union*/ #include<stdio.h> #include<conio.h> union example { int sno; char sname[20]; int marks; }e; main() { clrscr(); printf("n Sizeof sno:%d",sizeof(e.sno)); printf("n Sizeof sname:%d",sizeof(e.sname)); printf("n Sizeof marks:%d",sizeof(e.marks)); printf("n Sizeof Union is:%d",sizeof(e)); getch(); }
  • 8. CSC COMPUTER EDUCATION, M.K.B.NAGAR Unions of Structures It is possible to nest unions within unions, unions in structures, and structures in unions.
  • 10. CSC COMPUTER EDUCATION, M.K.B.NAGAR Structure Arrays It is an single entity representing a Collection of data types of different data types It is an single entity representing a Collection of data types of same data types Individual entries in a structure are called Members Individual entries in a array are called array elements The members of a structure are not stored in sequence of memory locations The members of a array are stored in sequence of memory locations All the members of a structure can be initialized Only the first member of an union can be initialized Individual structure elements are referred through the use of .(dot or membership) operator Individual array elements are accessed by its name followed by the square braces[] within which the index is placed Initialization of elements can be done only during structure definition Initialization of elements can be done during array declaration Structure definition reserve enough memory space for its members Array declaration reserve enough memory space for its elements The keyword ‘struct’ tells us what we are dealing with structure The is no keyword to represent arrays
  • 12. CSC COMPUTER EDUCATION, M.K.B.NAGAR Structure Union Keyword : struct Keyword : union Each member in a structure occupies and uses its own memory space All the members of a union use the same memory space More memory space is required since each member is stored in a separate memory locations Less memory space is required since all member is stored in the same memory locations All the members of a structure can be initialized Only the first member of an union can be initialized The variables that make up the structure are called Structure variable The variables that make up the union are called union variable Individual structure elements are referred through the use of .(dot or membership) operator Individual union elements are referred through the use of .(dot or membership ) operator Structure is a user-defined data type. Union is a user-defined data type. Possible to have one structure within another structure Unions within union is possible
  • 13. CSC COMPUTER EDUCATION, M.K.B.NAGAR Session Summary  The union is another compound data type may hold the variables of different types and sizes with the compiler keeping track of the size. The memory space reserved for a union members is large enough to store its largest member  The keyword “union” begins the union declaration followed by the tag (i.e., union name)  Within the braces union members are declared
  • 14. CSC COMPUTER EDUCATION, M.K.B.NAGAR EXERCISES 1. Define a union called student that will describe the following information student name,class,rollno, subject marks & total. Using student declare an array stu_list with 30 elements. Write program in C to read the information about all the 30 students and to display the information?