Union in C

Advertisements

Prev Tutorial Next Tutorial

Union in C Programming Language

A union is quite similar to the structures in C. It also store different data types in the same memory location. It is also a user defined data type same like structure.

union and structure are almost same

StructureUnion
struct student
{
int roll;
char name[10];
float marks;
}u;
union student
{
int roll;
char name[10];
float marks;
}u;

Defining a union

Union can be defined in same manner as structures, for defining union use union keyword where as for defining structure use struct keyword.

Syntax of Union in C

union tagname
{
datatype member1;
datatype member2;
.......
.......
};

Example of Union in C

union emp
{
int ID;
char name[10];
double salary;
}u;

Accessing members of an union

The member of unions can be accessed in similar manner as Structure with union reference. Suppose, we you want to access name variable in above example, it can be accessed as u.name.

Advantage of union over structure

It occupies less memory because it occupies the memory of largest member only.

Disadvantage of union over structure

It can store data in one member only.

Difference between Structure and Union

StructureUnion
1For defining structure use struct keyword.For defining union we use union keyword
2Structure occupies more memory space than union.Union occupies less memory space than Structure.
3In Structure we can access all members of structure at a time.In union we can access only one member of union at a time.
4Structure allocates separate storage space for its every members.Union allocates one common storage space for its all members. Union find which member need more memory than other member, then it allocate that much space

Memory Allocation in Structure and Union

Structure allocates separate storage space for its every members. Union allocates one common storage space for its all members. Union find which member need more memory than other member, then it allocate that much space

In case of Structure

Syntax

struct emp
{
int ID;
char name[10];
double salary;
};

Example

For above structure, memory allocation like below.
int ID -- 2B
char name[10] -- 10B
double salary -- 8B 
Total memory allocation = 2+6+8 = 16 Bytes

In case of Union

Syntax

union emp
{
int ID;
char name[10];
double salary;
};

For above union, only 8 bytes of memory will be allocated because double data type will occupy maximum space of memory over other data types.
Total memory allocation = 8 Bytes

When use Structure and Union

When need to manipulate the data for all member variables then use structure. When need to manipulate only one member then use union.

Note:

  1. All the properties of the structure are same for union, except initialization process.
  2. In case of structure initialize all data members at a time because memory location are different but in case of union only one member need to be initialize.
  3. In case of union if we initializing multiple member then compiler will gives an error.

Example of Union in C

#include<stdio.h>
#include<conio.h>

union emp
{
int ID;
char name[10];
double salary;
}u; //  reference of union

void main()
{
clrscr();
printf("Enter emp Id: ");
scanf("%d",&u.ID);
printf("Enter emp Name: ");
scanf("%s",&u.name);
printf("Enter emp Salary: ");
scanf("%f",&u.salary);
printf("Emp ID: %d",u.ID);
printf("Emp Name: %s",u.name);
printf("Emp Salary: %f",u.salary);
getch();
}

Output

Output:
Emp ID: 100
Emp Name: Porter
Emp Salary: 20000

online calculator


Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements