In this article Union in C we give the information about Union is a user defined data type by which we store different data types in the same memory location.

Union in C:

In C Language, Union is a user defined data type by which we store different data types in the same memory location.

Other words, “A union is a data type defined by the user. In this, all the members share the same memory location.”

We can define Union with many elements and each element in it is called a member.

It is defined by the union keyword.

Unions are similar in structure but there is a difference between them. Structure provides memory space to store all the members whereas union provides memory to store only the largest member.

We can access only one member of the union at a time. Not all members can be accessed at one time.

Define Union:

Union keyword is used to define it. The way we define the structure, we do it in the same way.

syntax:-

union union_name

{

//member definitions

Return_Type member1_variable;

Data_Type member2_variable;

Data_Type member3_variable;

};

  • union is a keyword with the help of which we create a new data type
  • union_name – This is the name of the union data type, you can keep this name according to your own like – employee, college, item etc.
  • DataType – Int, char, float, double anything can come in DataType.
  • member1_variable – This is the name of the variable. Every element inside the union is called a member.

Example:

union item

{

int x;

float y;

char z;

};

Note -:

The size of union is equal to the size of the largest data member of the union. for example – If a union has union members of 1, 2, 4, 8, 16 bytes, then the size of the union will be 16 bytes.

Declare Union Variables:

Here we will declare the union variable in the same way as we declare the structure variable in C language. We declare the union variable so that we can easily access the union member and use it anywhere in our program.

union item

{   int x;

float y;

char z;

};

Here union is the keyword and item is a new user defined data type inside which three union members named x, y and z are declared with the help of int, float and char. If we want to access these three members, then for this we will have to declare a variable of this item data type.

In C language, we can declare a union variable like a structure in two ways -:

  • Through the union keyword inside the main() function.
  • At the end of union only while defining union.

Method 1 -: By union keyword inside main() function

Example -:

#include<stdio.h>

union item

{

int x;

float y;

char z;

};

void main()

{

union item p, q;

}

To access the union member, we have to declare the union variable. So here in this example we have used union item p, q; in main() function to access the union member; By declaring two union variables named p, and q, with the help of which we can easily access the union member.

Method 2 -: At the end of the union while defining the union.

Example -:

union item

{   int x;

float y;

char z;

}p,q;

We can also declare the union variable like the example given above.

  • Dot(.) operator is used to access the union member and if a union pointer variable wants to access the union member then -> (structure pointer operator) is used instead of the dot(.) operator. goes |

Initialization of Union Members

#include<stdio.h>

union item

{   int x;

float y;

char z;

};

void main()

{

union item p;

//initialization of each member separately

p.x = 305;

p.z = ‘u’;

p.y = 115.5;

}

Accessing Members in Union 

Like the structure, we can also access the union member in these two ways.

  • By Dot(.) (member or dot operator)
  • -> (union pointer operator)

Method 1 -: By Dot(.) operator

By Dot(.) operator, we can initialize any value according to our own access to the union variables and also print them in the screen.

Examples -:

#include<stdio.h>

union item

{   int x;

float y;

};

void main()

{

union item p;

//initialization of each member separately

p.x =  110;

printf( ” x = %d \n”,p.x);

p.y = 115.5;

printf( ” y  = %f “, p.y);

}

Output -:

x = 110

y =  115.5

Note -: At a time we can access only one union member. If we try to access more than one union member at a time, less error comes.

Method 2: By -> (union pointer operator)

We can easily access the union member by -> (union pointer operator). Let us understand an example of this -:

Example Program

#include <stdio.h>

union test

{

int x;

char y;

};

void main()

{

union test p;

p.x = 66;

// p2 is a pointer to union p1

union test* q = &p;

// Accessing union members using pointer

printf(“%d %c”, q->x, q->y);

}

Output -:

66 B

In this example we created two pointer variables p, q and used -> to access the union member through pointer variables. Since the ASCII value of 66 is B, the value of p is printed as 66 and that of q is B.

Advantages of Union

It takes up a lot of memory space as compared to the structure.

Using Union, you can access only the last member directly.

Union is more useful when you have to store the values ​​of multiple data members sharing the same memory location.

Its size is equal to the size of the largest union member.

Disadvantages of Union

Can access only one member at a time.

Can’t use more than one union member simultaneously.

A memory location is shared among more than one union member.

Union Program in C:

#include <stdio.h>

union demo

{

int p;

float q;

char ch;

};

int main( )

{

union demo st;

st.p = 100;

st.q = 300.2;

st.ch = ‘Z’;

printf(“%d\n”, st.p);

printf(“%f\n”, st.q);

printf(“%c\n”, st.ch);

return 0;

}

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *