A union is a collection of variables of different types, just like a structure. However, with unions, you can only store information in one field at any one time.
UNION
A
union is a collection of variables of different types, just like a structure.
However, with unions, you can only store information in one field at any one time.
You
can picture a union as like a chunk of memory that is used to store variables
of different types. Once a new value is assigned to a field, the existing data
is wiped over with the new data.
A
union can also be viewed as a variable type that can contain many different
variables like a structure, but only actually holds one of them at a time not
like a structure. This can save memory if you have a group of data where only
one of the types is used at a time. The size of a union is equal to the size of
its largest data member. In other words, the C compiler allocates just enough
space for the largest member. This is because only one member can be used at a
time, so the size of the largest, is the most you will need.
Union
is a derived data type and it is declared like structure. The difference
between union and structure is in terms of storage. In structure each member
has its own storage location, whereas all the members of union use the same
location, although a union may contain many members of different types. When we
use union the compiler allocates a piece of storage that is large enough to
hold. Like structures, union is also declared by using the keyword union. Syntax:
union union_name
{
union member1;
union member2;
…………
union membern;
};
union union variable;
Example:
union result
{
int marks;
float avg;
char grade;
}std;
The
example shows above declares std of type union results, this contains three
union members each different type. However, only one member can be used at a
time, since one location is allocated for union variable without considering
its size.

Example 1: Program to
union global declarations.
/* Program using union */
/* Global declarations */
#include <stdio.h>
union name
{
int a;
char b[2];
};
main()
{
/* Local definitions */
union name c;
c.a=256;
printf("c.a value is %d\n", c.a);
printf("c.b[0] value is %d\n",c.b[0]);
printf("c.b[1] value is %d\n", c.b[1]);
}
OUTPUT
c.a value ....256
c.b[0] value ....0
c.b[1] value ....1
EXPLANATION:
In the above program the union is declared as union name name. c is a variable
to union name. c.a is initialised as 256. Then prints the c.a and location of
c.b[0], c.b[1] as output.
Example 2: Program to
find size of union and number of bytes reserved for it.
/* Program_to find size of union and number of bytes */
#include <stdio.h>
void main()
{
union result /* result is a union with elements */
{
int marks;
char grade;
};
struct res /* res is a structure with elements */
{
char name[15];
int age;
char sex;
char address;
int pincode;
union result
perf;
}
/* Statements */
clrscr();
printf("Size of Union: %d\n",sizeof(data.perf));
printf("Size of Structure: %d\n",sizeof(data));
getch();
} /* main */
OUTPUT
Size of Union : 2
Size of Structure : 23
EXPLANATION:
The above program refers to fine size of union and size of structure. The union
is declared with name 'result'. The structure is declared with name 'res',
within the structure the declared union 'result' is accessed through variable
'perf'. Prints the size of union ‘sizeof(data.perf)' and prints the size of
structure using 'sizeof(data)' functions.
Example 3: Program to
demonstration on pointer to unions.
/* Program to demonstration on pointer to unions */
#include <stdio.h>
#include <conio.h>
typedef union
{
int num;
char *chr;
float dec;
}rec;
int main()
{
/* Local definition
*/
rec uniondata; /*
declare union of the rec type */
rec *ptr; /* &union pointer of the rec
type */
ptr =&uniondata; /* now point to the first union*/
uniondata.num = 10;
printf("The
union value is now: %d\n", ptr‒> num);
uniondata. chr =
"ten";
printf("The
union value is now: %s\n",ptr‒> chr);
uniondata.dec
0.250;
printf("The
union value is now: %f\n",ptr‒> dec);
return 0;
} /* main */
OUT PUT
The union value is
now: 10
The union value is
now: ten
The union value is
now: 0.250000
EXPLANATION:
This program defines a union type that contains an int, char or float value. A
union and a pointer are declared, then various values are assigned to the
union. Each one is assigned to the regular union then retrieved by the pointer,
using the arrow operator.
Difference
between union and structure Though unions are similar to structure in so many
ways, the difference between them is crucial to understand. This can be
demonstrated by this example:
#include <stdio.h>
union job /*defining
a union */
{
int empno;
char name[80];
floatsalary;
}u;
struct jobl
{
int empno;
char name[80];
float salary;
}s;
int main()
{
printf("size
of union = %d",sizeof(u));
printf("\nsize
of structure = %d", sizeof(s));
return 0;
}
OUTPUT
size of union = 80
size of structure = 86

The
structure and union are compared as follows:
•
Every member has its own memory space.
•
Keyword struct is used.
•
Any member can be accessed at any time without the loss of data.
•
More storage space is required.
•
It can handle all the members or a few as required at a time.
•
It may be initialized with all its members.
•
Different interpretation for the same memory location is not possible.
•
All the members use the same memory space to store the values.
•
Keyword union is used.
•
Different interpretations for the same memory location are possible.
•
It can handle only one member at a time, even all the members use the same
space.
•
Only its first members may be initialized.
•
Conversion of memory is possible.
'C' provides a capability that enables the
programmer to assign an alternate name to a data type. This is done with a
statement known as typedef.
It
is a declaration with typedef as the storage class. The declarator becomes a
new type. We can use typedef declarations to construct shorter or more meaningful
names for the date types, which were already defined by C, or for types that we
have declared, typedef names allow us to encapsulate implementation details
that may change.
A
typedef declaration is interpreted in the same way as a variable or function
declaration, but the identifier, instead of assuming the type specified by the
declaration, becomes a synonym for the type.

Here
weeks is another name for int and now we can use weeks instead of int in the
program as follows.
Example:
weeks wk;
Example: Program to
create user defined data type weeks on int data type and used it in the
program.
/* Program to create user defined data type and use it in the
program */
#include <stdio.h>
#include <conio.h>
#define D 7
void main()
{
/* Local definitions */
typedef int weeks;
weeks wk;
/* Statements */
clrscr();
printf("Enter weeks: ");
scanf("%d",&wk);
printf("Number of days = %d", wk*D);
getch();
} /* main */
OUTPUT
Enter weeks: 4
Number of days = 28
EXPLANATION:
In the above program with typedef we have declared weeks as an integer
datatype. Immediately, after the typedef statement wk is a variable of weeks
datatype which is similar to int. The program calculate days using wk variable.
Particularly,
the typedef feature is convenient when definining structures, since it
eliminates the need to repeatedly write struct tag whenever a structure is
referenced, so the structure can be referenced more concisely.

Example: Program to create user‒defined
datatype from structure.
/* Program to create user‒defined datatye */
#include<stdio.h>
#include<conio.h>
void main()
{
/* Local definitions */
typedef struct
{
char name[20];
char gender[2];
int age;
}rec;
rec candidate[2];
int a;
/* Statement */
clrscr();
for(a=0;a<2;a++)
{
printf("Name
of the Employee: ");
scanf("%s",
candidate[a].name);
printf("Gender:
");
scanf("%s",
candidate[a].gender);
printf("Age:
");
scanf("%d",
&candidate[a].age);
}
printf("\nName\t Gender\t Age\n");
for(a=0;a<2;a++)
{
printf("%s\t",
candidate[a].name);
printf("%s\t",
candidate[a].gender);
printf("%d\n",
candidate[a].age);
}
getch();
} /* main */
OUTPUT
Name of the Employee:
Mahi
Gender: m
Age: 20
Name of the Employee:
Suma
Gender: f
Age: 25
Name Gender
Age
Mahi m 20
Suma f 25
EXPLANATION:
In the above program, user‒defined data type rec is created using typedef
statement. The rec datatype consists of two character and one integer field. An
array candidate[2] is declared based on rec datatype. The first for loop and
scanf() statement reads data. The second for loop and printf() statement prints
the content of array element.
Computer Programming C: UNIT IV: Structures and Unions : Tag: Computer Science : C Programming - Union
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation