Short Notes: Union | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE) PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


UNION
Union is a collection of variables of different data types, in case of union information can only be 
stored In one field at any one time. A union is a special data type available in C that enables you 
to store different data types in the same memory location. You can define a union with many 
members, but only one member can contain a value at any given time. Unions provide an efficient 
way of using the same memory location for multi-purpose.
Declaring Union
union union-name
{
data type var-name; 
data type var-name;
};
The union tag is optional and each member definition is a normal variable definition, such as int 
i; or float f; or any other valid variable definition. At the end of the union's definition, before the 
final semicolon, you can specify one or more union variables but it is optional. Here is the way 
you would define a union type named Data which has the three members i, f, and str. Now, a 
variable of Data type can store an integer, a floating-point number, or a string of characters. This 
means that a single variable ie. same memory location can be used to store multiple types of data. 
You can use any built-in or user defined data types inside a union based on your requirement.
The memory occupied by a union will be large enough to hold the largest member of the union. 
For example, in above example Data type will occupy 20 bytes of memory space because this is 
the maximum space which can be occupied by character string. Following is the example which 
will display total memory size occupied by the above union:
Accessing a Member of a Union
#include <stdio.h>
#include <string.h> 
union Data 
{
Page 2


UNION
Union is a collection of variables of different data types, in case of union information can only be 
stored In one field at any one time. A union is a special data type available in C that enables you 
to store different data types in the same memory location. You can define a union with many 
members, but only one member can contain a value at any given time. Unions provide an efficient 
way of using the same memory location for multi-purpose.
Declaring Union
union union-name
{
data type var-name; 
data type var-name;
};
The union tag is optional and each member definition is a normal variable definition, such as int 
i; or float f; or any other valid variable definition. At the end of the union's definition, before the 
final semicolon, you can specify one or more union variables but it is optional. Here is the way 
you would define a union type named Data which has the three members i, f, and str. Now, a 
variable of Data type can store an integer, a floating-point number, or a string of characters. This 
means that a single variable ie. same memory location can be used to store multiple types of data. 
You can use any built-in or user defined data types inside a union based on your requirement.
The memory occupied by a union will be large enough to hold the largest member of the union. 
For example, in above example Data type will occupy 20 bytes of memory space because this is 
the maximum space which can be occupied by character string. Following is the example which 
will display total memory size occupied by the above union:
Accessing a Member of a Union
#include <stdio.h>
#include <string.h> 
union Data 
{
int i; 
float f; 
char str[20];
};
int main()
{
union Data data; 
data.i = 10; 
data.f = 220.5;
strcpy(data.str, "C Programming"); 
p rin tf "data.i: %d\n", data.i); 
p rin tf "data.f: %fn", data.f); 
p rin tf "data.str : %s\n", data.str); 
return 0;
}
Dot operator can be used to access a member of the union . he member access operator is coded 
as a period between the union variable name and the union member that we wish to access. You 
would use union keyword to define variables of union type. Following is the example to explain 
usage of union:
Exercises:
1 . Write a program to define a union and a structure both having exactly the same members. 
Using the sizeof operator, print the size of structure variable as well as union variable and 
comment on the result.
2. Write a program to define a structure for a hotel that has the member’s mane, address, 
grade, number of rooms, and room charges. Write a function to print the names of the 
hotels in a particular grade. Also write a function to print names of a hotel that have room 
charges less than the specified value.
Read More
90 docs

Top Courses for Computer Science Engineering (CSE)

Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

study material

,

pdf

,

Sample Paper

,

Exam

,

Objective type Questions

,

mock tests for examination

,

Viva Questions

,

video lectures

,

Important questions

,

practice quizzes

,

Short Notes: Union | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Short Notes: Union | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Free

,

Summary

,

Short Notes: Union | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

MCQs

,

Previous Year Questions with Solutions

,

shortcuts and tricks

,

Semester Notes

,

ppt

,

past year papers

,

Extra Questions

;