What is UNION in C language?

Introduction

A union is a user-defined datatype, where we will discuss what is the union in c? How is a member of a union variable assigned an initial value? For what kind of applications are union useful and how is a union member accessed in the c program?

Getting Started

The union is a user-defined datatype in which all members share the same memory location. At any given time, a union can contain no more than one object from its list of members. It is declared just like structure variables. Union allocates maximum datatype variables size. Rest small data type variables reuses the same memory space.

Union members can’t be initialized within the union declaration body. Initialization of the member variables within a union gives raise to error. Initialization of the union variable can be done after the class declaration when objects are declared.

We have to follow the sequence in which the variables are defined. If any value is absent then the default value is taken as the initial value. The following is an example of a declaration and initialization of union.

 //declaration of union  
 union Student {   
   char name[30];   
   char branch[25];   
  };   
   
  //Initialization of union.  
  union Student student;      
   student.name = "Raman";  
   student.branch = "IT";  

In the above example name is the bigger datatype variable. So the size of the union equal to the size of the “name” variable. Rest small size variables will reuse the same memory space.

Union members are accessed by the object of the structure. If the object is a general object, then the members are accessed using the dot operator and if the object is a pointer type, then members are accessed using an arrow operator.

 //declaration of union  
 union Student {   
   char name[30];   
   char branch[25];   
  }; s1, *s2  

In the above example, the “s1” is a general object, and “*s2” is a pointer object. So “name” member can be accessed by”s.1.name” using general object “s1” and “s2→name” using pointer object “*s2”.

Application:-

  1. Used for production of machine-independent code because the compiler keeps track of the actual size of the variables which make up the union.
  2. Unions are used when type conversions are needed because the data held in a union can be referred to in different ways.

Program:-

 #include <stdio.h>  
 #include <string.h>  
  //Declaration of union  
 union Student {  
   char name[30];  
   char branch[25];  
 };  
    
 int main( ) {  
      //Initialization of union  
   union Student student;      
   student.name = "Raman";  
   student.branch = "IT";  
     
   printf( "Student Name : %d\n", student.branch);  
   printf( "Student Branch : %f\n", student.branch);  
   return 0;  
 }  

A union can be useful for conserving memory when you have lots of objects and limited memory. However, a union requires extra care to use correctly. You're responsible for ensuring that you always access the same member you assigned. If any member types have a non-trivial constructor, then you must write additional code to explicitly construct and destroy that member. Before you use a union, consider whether the problem you're trying to solve could be better expressed by using a base class and derived class types.

Summary

in the above, we discussed what is the union in c? How is a member of a union variable assigned an initial value? For what kind of applications are union useful and how is a union member accessed in the c program? I hope you have enjoyed it a lot.

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال