Union in C





Union ?

  • Union is conceptually similar to structure,the only differences is in terms of storage.

  • A union is a special user defined data type available in C that allows to store different data variables in the same memory location.

  • A union is a memory location that is shared by two or more variable generally of different types at different time.

Defining a Union

To define a structure, you must use the union keyword. The format of the union statement is given below:

Syntax:

union union_name
 {
   data_type member1;
   data_type member2;
    .
    .
   data_type memeber;
 }[one or more union variable];
                

Difference between Structure and Union

Though union is similar to structure in so many ways, it is important to understand the difference.

The main difference is in terms of storage. In structure every member of structure has its own storage location, so the size of structure type variable is the sum of its all member's size, whereas all members of union uses the single memory location which is equal to the size of its largest data member, so the size of union type variable is equal to the largest data member size.

See the program :

#include<stdio.h>
struct Student {
   int roll_no;
   char  name[40];
};
union Student1 {
   int roll_no;
   char  name[40];
};

int main( ) {
  struct Student s;
  union Student1 s1;
  
  printf("SIZE OF STRUCTURE : %d",sizeof(s));
  printf("\nSIZE OF UNION : %d",sizeof(s1));
  return 0;	
}
Output:
SIZE OF STRUCTURE : 44
SIZE OF UNION : 40

Since memory is shared by union members,only one union member can be accessed at a time

#include<stdio.h>

union Product {
   int s_no;
   float  price;
   int quantity;
};
int main( ) {
  union Product p;
  
  p.s_no = 1;
  p.price = 20.0;
  p.quantity = 3;
  
  printf("Product Deatil :");
  printf("\n S_NO : %d",p.s_no);
  printf("\n PRICE : %f",p.price);
  printf("\n QUANTITY : %d",p.quantity);
  return 0;	
}
Output :
Product Deatil :
S_NO : 3
PRICE : 0.000000
QUANTITY : 3

As you can see in the example value of s_no and price is not showing exactly as you have saved, only the last value qunatity is showing the correct value.
This is because in union, the memory is shared among different data types. Hence, the only member whose value is currently stored will have the memory.


Next topic is typedef and enum





 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved