Definition: Union is a user defined data structure which is just similar to structure. It is used to store members of different data types.
Union
AU:
May-19, Marks 13
Definition:
Union is a user defined data structure which is just similar to structure. It
is used to store members of different data types.
'C'
allows another type of structure, the union, which allows the variables
to be interpreted in several different ways.
For
example: Consider a structure giving the details of student.
Such as his name, roll number and standard. The categorisation of the standard
will be primary or pre-primary. The primary standard will be denoted by 1, 2
till 4 standards. The pre-primary standard will be denoted by "Play
group", "Jr. K.G.", and "Sr. K.G." respectively.
Let
us see, how it can be denoted in 'C'.
struct
student
{
char
name[20];
int
roll_no;
union
{
int
primary;
char
pre_pri [10];
}std;
};
For
example : If a record of student having name - Rama, roll number
as 5 and he is in 3rd standard. It will be stored in the following manner.
Similarly
a record having name Shyam roll number 2 and standard as Jr. K.G will be mapped
as
The
structure and the union both are very similar data structures. Both are meant
for storing the various member variables of different data types. But the only
difference between the two is that various members of the structure stored at
various is memory locations. And in case of unions, all the members are stored
at the same memory location and that is why only one member can be accessed at
a time by the unions variable. You just observe the above given example, the
union is defined for the standards primary and pre-primary. Note that the same
location is utilized to store the data of different data types. But at a
time you can access only one data type of union. In case of above
example, the memory location for std will hold either primary data or
pre-primary data. The dot operators (exactly like the structure) are used to
access the members of the union.
Ex.
2.2.1 Define union having array of characters of size 2, one integer and one
float as its elements.
Sol.
:
union
{
char
a[2];
int
i;
float
j;
};
Ex.
2.2.2 Illustrate the representation of structure and union for an employee
record having empid, emp-name, DOB, basic pay, allowances, deductions, grosspay
and netpay. Examine their memory allocation.
AU
May-19, Marks 13
Sol.
:
Structure allocates different memory locations for all its members while union
allocates common memory location for all its members. The memory occupied by a
union will be large enough to hold the largest member of the union.
For
example - Consider following C program with union w
#include<stdio.h>
union
Employee
{
int
empid;
char
empName[30];
char
dob[20];
float
basic_pay;
float
allowances;
float
deduction;
float
grosspay;
float
netpay;
};
int
main( )
{
union
Employee E;
printf("\nEnter
Employee Id : ");
scanf("%d",
&E.empid);
printf("\nEnter
Employee Name: ");
scanf("%s",
&E.empName);
printf("\nEnter
Employee dob: ");
scanf("%s",
&E.dob);
printf("\nEnter
Employee basic_pay: ");
scanf("%f",&E.basic_pay);
printf("\nEnter
allowances: ");
scanf("%f",&E.allowances);
printf("\nEnter
deductions: ");
scanf("%f",&E.deduction);
E.grosspay
= (E.basic_pay+E.allowances);
E.netpay
= (E.basic_pay+E.allowances);
-
E.deduction;
printf("\n\nEmployee
Id : %d",E.empid);
printf("\nEmployee
Name: %s",E.empName);
printf("\nEmployee
DOB: %s",E.dob);
printf("\nEmployee
gross Salary: %f",E.grosspay);
printf("\nEmployee
Net Salary: %f",E.netpay);
return
0;
}
Output
Enter
Employee Id: 101
Enter
Employee Name: AAA
Enter
Employee dob: 12-10-01
Enter
Employee basic_pay : 10000
Enter
allowances : 2000
Enter
deductions : 500
Employee
Id: 1148846080
Employee
Name :
Employee
DOB:
Employee
gross Salary: 10000.000000
Employee
Net Salary: 0.000000
Note
that only one memory location will be used by union at a time, rest of the
locations are treated as garbage. But in structure it is not so, for each
member the structure will allocate the separate memory location
Consider
following C program with structure
#include<stdio.h>
struct
Employee
{
int
empid;
char
empName[30];
char
dob[20];
float
basic_pay;
float
allowances;
float
deduction;
float
grosspay;
float
netpay;
};
int
main( )
{
struct
Employee E;
printf("\nEnter
Employee Id : ");
scanf("%d",
&E.empid);
printf("\nEnter
Employee Name : ");
scanf("%s",
&E.empName);
printf("\nEnter
Employee dob: ");
scanf("%s",
&E.dob);
printf("\nEnter
Employee basic_pay: ");
scanf("%f",&E.basic_pay);
printf("\nEnter
allowances: ");
scanf("%f",&E.allowances);
printf("\nEnter
deductions: ");
scanf("%f",&E.deduction);
E.grosspay
(E.basic_pay+E.allowances);
E.netpay
= (E.basic_pay +E.allowances)
-
E.deduction;
printf("\n\nEmployee
Id : %d", E.empid);
printf("\nEmployee
Name: %s",E.empName);
printf("\nEmployee
DOB: %s",E.dob);
printf("\nEmployee
gross Salary: %f",E.grosspay);
printf("\nEmployee
Net Salary: %f",E.netpay);
return
0;
}
Output
Enter
Employee Id : 101
Enter
Employee Name: AAA
Enter
Employee dob: 12-10-01
Enter
Employee basic_pay : 10000
Enter
allowances : 2000
Enter
deductions : 500
Employee
Id: 101.
Employee
Name : AAA
Employee
DOB: 12-10-01
Employee
gross Salary: 12000.000000
Employee
Net Salary: 11500.000000
C Programming and Data Structures: Unit II: C Programming – Advanced Features : Tag: : Definition, Syntax, Initializing, Example C programs - Union
C Programming and Data Structures
CS3353 3rd Semester EEE, ECE Dept | 2021 Regulation | 3rd Semester EEE Dept 2021 Regulation