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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …