Pages

January 23, 2013

Union in C-language


practical example of union in day to day life in programming

Union is that data structure which is also user defined data type but to define a union keyword union  is used instead of struct.


Data members of the union can also be used as heterogeneous data i.e. different types of data can be accessed and it is defined in same manner in which structure is defined. 

Format is:
union  name  
{
   int num1;
   double num2;
   float num3;
 };
void main()
{
   ……………;
  union name n;
   …………….;
  }

DIFFERENCE BETWEEN STRUCTURE AND UNION

The basic difference between structure and union is that union uses common memory area to store all its members but structure uses different memory area to store each of its members.

It means union takes less memory as compare to structures for same data members.

But union will store only the latest record. It can’t store all the records just like structure. It inputs data and there after output data side by side and members of the union are accessed in same manner in which member of structure are accessed.

No comments:

Post a Comment