SlideShare una empresa de Scribd logo
1 de 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?

Más contenido relacionado

Similar a Understanding Unions and Structures in C

Similar a Understanding Unions and Structures in C (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++
 

Último

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
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
 
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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
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
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Último (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
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...
 
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
 
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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
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
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

Understanding Unions and Structures in C

  • 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?