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 check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More