Union in C – All you need to know!

Filed Under: C Programming
C UNION

Hey, all. Today, we discuss one of the most advanced topics in the list of C programming – Union.


Understanding Union in C

Union in C, is a user defined datatype that enables the user to store elements of different types into it altogether.

We can define various data members/variables within, but only one data variable can occupy the memory in the union at a time.

Thus, we can say that the size or the memory of the union is equivalent to the size of the largest data member present in it. So, all the data variables share the same memory location in the Union.

By this, Union provides an effective way to reuse the same amount of memory or space for multiple purposes.

With the basics covered, let’s define a Union next.


How to define a Union in C?

The union helps us define data variables that store values of different data type as shown below:

Syntax:

union union-name {
   data member1 definition ;
   data member2 definition;
   ...
   data memberN definition;
}  
  • The keyword union is used to declare a union structure.
  • Further, the union-name is the name of the union that helps us access the union throughout the program.
  • We can define multiple data members of different data types within a union.

Example:

union Info {
   int roll;
   float marks;
   char name[50];
} info_obj;  

In the above example, we have created a union ‘Info’ and defined the variables of different data types such as int, float, char, etc.

Union uses the same memory space/size to store values of different data types which increasing the reusability of the space.

The memory space of the union is the size of the largest data member declared within it.

In the above example, the largest variable ‘name’ occupies a space of 50 bytes. Thus, the union ‘Info’ would also represent a memory space of 50 bytes.


Creating Union Variables

A union variable helps the union type to allocate memory space in accordance with the largest data member.

There are two ways to create a union variable:

Method 1:

union union-name {
   data member1 definition ;
   data member2 definition;
   ...
   data memberN definition;
}  union_variable 1, union_variable2, .... , union_variableN;

Method 2:

union union-name {
   data member1 definition ;
   data member2 definition;
   ...
   data memberN definition;
};

int main()
{
  union union-name union_variable 1, union_variable2, ... ,union_variableN;
  return 0;
}

Accessing Union Data Members

In order to assign values to the data members defined in the union type, we need to access them using the union variables.

There are two techniques to access the union data members:

Method 1: Using the dot (.) operator

union_variable.data_member = value;

Method 2: Using the Arrow (->) operator

union_variable->data_member = value;

Let us have a look at the below example to understand the above explained concepts.

#include<stdio.h>
union Info {
   int roll;
   float marks;
   char name[100];
} info_obj;  

int main() {
   info_obj.roll = 1;
   info_obj.marks = 82.5;
 
   printf("Roll number :  %d\n", info_obj.roll);
   printf("Marks obtained :  %.1f",  info_obj.marks);
   return 0;
}

Demonstrated in the output below, only the last variable, ‘marks’, gets displayed properly.

That’s because, as you can witness the variables ‘marks’ and ‘roll’ are being accessed by the variable ‘info_obj’ at the same time. Since the memory location is shared, only the last value gets hold of the memory during output. The previously declared variable ‘roll’ gets corrupted.

This serves to prove our initial statement about the Union data type sharing memory location.

Output:

Roll number :  1118109696
Marks obtained :  82.5

Have a look at the below example to see the difference!

#include<stdio.h>
union Info {
   int roll;
   float marks;
   char name[100];
} info_obj;  

int main() {
   info_obj.roll = 1;
   printf("Roll number :  %d\n", info_obj.roll);
   info_obj.marks = 82.5;
   printf("Marks obtained :  %.1f",  info_obj.marks);
   return 0;
}

Now, both the data variables ‘roll’ and ‘marks’ get displayed properly. The reason is, here the union variable ‘info_obj’ accesses one variable at a time and displays the value and then moves ahead to fetch another data member.

Output:

Roll number :  1
Marks obtained :  82.5

Difference Between a Structure and a Union

Consider the below example, to understand the difference between a Structure and a union:

#include <stdio.h>  
union u_Info {
   int roll;
   float marks;
   char name[100];
} u_obj;  

struct s_Info {
   int roll;
   float marks;
   char name[100];
} s_obj;  

int main()
{
   printf("Size -- Structure datatype = %d\n", sizeof(s_obj));
   printf("Size -- Union datatype = %d", sizeof(u_obj));
   return 0;
}

Union occupies the memory space of the largest variable present in it. Here, the variable ‘name’ has 100 bytes of space. Thus, the size of union is 100 bytes.

On the other hand, Structure occupies the memory size which is the summation of the size of all the data members present in it. Thus, structure represents the memory size of 108 bytes.

Output:

Size -- Structure datatype = 108
Size -- Union datatype = 100

Conclusion

By this, we have come to the end of this topic. In this article, we have understood the working of Unions and the difference between a Union and a Structure. Feel free to comment below, in case you come across any question.

For more such posts related to C programming, do visit C programming with JournalDev.


References

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages