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

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 …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More