Union in C

C Union

  • Unions are quite similar to the structures in C. 
  • Union is also a derived type as structure. Union can be defined in same manner as structures just the keyword used in defining union in union where keyword used in defining structure was struct.
union car
{
  char name[50];
  int price;
};

  • Union variables can be created in similar manner as structure variable.

union car
{
  char name[50];
  int price;
}c1, c2, *c3;

OR

union car
{
  char name[50];
  int price;
};


  • Only one union member can be accessed at a time 
  • You can access all members of a structure at once as sufficient memory is allocated for all members. However, it's not the case in unions. You can only access a single member of a union at one time. Let's see an example.

#include <stdio.h>
union Job
{
   float salary;
   int workerNo;
} j;
int main()
{
   j.salary = 12.3;
   j.workerNo = 100;
   printf("Salary = %.1f\n", j.salary);
   printf("Number of workers = %d", j.workerNo);
   return 0;

}

Defining union


  • The union keyword is used to define union. Let's see the syntax to define union in c.
union union_name   
{  
    data_type member1;  
    data_type member2;  
    .  
    .  
    data_type memeberN;  
};  

  • Let's see the example to define union for employee in c.
union employee  
{   int id;  
    char name[50];  
    float salary;  
};

C Union example

Let's see a simple example of union in C language.

#include <stdio.h>  
#include <string.h>  
union employee    
{   int id;    
    char name[50];    
}e1;  //declaring e1 variable for union  
int main( )  
{  
   //store first employee information  
   e1.id=101;  
   strcpy(e1.name, "mskuthar");//copying string into char array  
   //printing first employee information  
   printf( "employee 1 id : %d\n", e1.id);  
   printf( "employee 1 name : %s\n", e1.name);  
   return 0;  
}
Output:

employee 1 id : 1869508435
employee 1 name : mskuthar

As you can see, id gets garbage value because name has large memory size. So only name will have actual value.


-------Inside Function-----------

union car c1, c2, *c3;

  • In both cases, union variables c1, c2 and union pointer variable c3 of type union car is created.
Accessing members of an union:
  • The member of unions can be accessed in similar manner as that structure. Suppose, we you want to access price for union variable c1 in above example, it can be accessed as c1.price. 
  • If you want to access price for union pointer variable c3, it can be accessed as (*c3).price or as c3->price.
  • Like structure, Union in c language is a user defined datatype that is used to hold different type of elements.
  • But it doesn't occupy sum of all members size. It occupies the memory of largest member only. It shares memory of largest member.

Advantage of union over structure

  • It occupies less memory because it occupies the memory of largest member only.

Disadvantage of union over structure

  • It can store data in one member only.

Post a Comment

Previous Post Next Post