C – Union3 min read

Like Structure in C, Union is also a user-defined data type that is used to store the collection of different data types which are grouped together.

Union and structure both are the same, except allocating memory for their members. Structure allocates storage space for each member separately whereas, Union allocates one common storage space for all its members providing an efficient way of using the same memory location for multi-purpose.

Defining Union

The keyword union is used to define union. The syntax of union is stated below.

Applying above syntax we can write the union for student in following way in C.


Creating a Union variable

Union variable is created to allocate the memory then we can perform some task, defining union does not allocate memory. Union variable will always have the size of its largest member

Union variable can be created in two different ways:

1. Union variable can be created inside the main() function as shown blow.

2. Union variable can be created after defining the union as shown below.

In both s1 and s2 are union variables and s3 is a union pointer variable.


How to access the members of union?

Accessing the (.) dot operator is used to access the union members and -> operator is used to access the pointer variables.

Consider the above mention example of union. We can access its members in the following ways:

  • s1.st_id;
  • s2._st_name;
  • (*s3).st_address or s3->st_address

Example of Union in C programming

We will see two examples, first one will give a garbage value as only one at a time the memory is allocated to one member.

1. Union Program in C: shows garbage value

Output:

1. Union Program in C: proper result

Output:

Note that in the above example one member is being used at a time that’s why all the members are printed properly else the student id value might get corrupted like the one in first example.
Using one variable at a time is the main purpose of using ‘union’.


MORE

Implement Insertion sort Program in C

In this tutorial, we will learn and write a C program to implement insertion sort. Before that you may go through the following topics in …
Read More

C Program for implementation of Selection sort

In this tutorial, we will learn and write a C program to implement selection sort using function. Before that you may go through the following …
Read More

C Program to Delete a Particular element from an Array

In this tutorial, we will write a C program to delete an element in an array by specifying the value. Before that, you should have …
Read More

C Program to sort a String in Ascending order

In this tutorial, we will write a c program to sort a string array in ascending order. Before that, you may go through the following …
Read More

C Program to find Maximum occurring Character in a String

In this tutorial, we will write a program in C to find the maximum occurring character in a string. Before that, you may go through …
Read More

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More