Unions

  • Why Unions?
  • As you know 'C' language provide us with variable to store the single data ,Array for storing related type of data,then Why Union ?
  • Because, In real life there is a situation where we have store a data which is logical related to each other but they are not of same type.
  • They are related but ,Data contains Dissimilar information,which means each data is of different datatype.
  • For example you have to store a student information:
    then attributes of student will be Name ,roll_no,class,div,address,etc then how can you store this data by Using int ,char or by using simple array,So this not possible by predefined datatypes.
  • Now , at this type of situation Union comes in action.


What are Unions?


  • Unions are Programmer define Data types.
  • Unions is a collection of logically related data of different data types which are grouped together under a single name.
  • Unions are same like Structures , Now if you dont know what are structures than you can go through this topic: What are Structures?.
  • But, It is not necessary, that for learning Unions You should know what are structures.
  • Unions are just an another way of defining programmer defined data types.
  • There is slightly difference between structures and Unions and that difference is about storage.
  • In Structures each member stores the unique storage area in the computer memory.
  • But , all members of union uses a single shared memory location which is equal to the size of its largest data member
  • Look the below image to understand the concept:




  • As each member shares the same single memory, that is why only one members can be active at a time.
  • Also , you cannot Initialize the Union Variables.
  • You can use them when values of multiple members need not to be assigned to all the members at any one time.
  • Syntax: defining Unions.
    union union_name
    {
        datatype  member1; // each item  in union is member
        datatype  member2;
        ......
        ......
    };
    

    Note: union is a keyword.
    while defining union you have to use that keyword.

  • Let's define our first union.
    We will define a Union for storing data of student.


    Definition:
    union student  //union name
    {
        int  rollno; //member1 ,integer variable which will store,
                     //student roll no.
                     
        char  name[20]; //member2 a character array of 20 character
                        //which will store name of student.
                        
        char  div;  //member3 a character variable,
                  //which will store division of student.
    };
    

    • In Union student , each member will share 20 bytes ,because it is the size of largest data of member name.




  • Q:Define a union employee and it should have employee name,employee id,employee designation,and employee salary as it's members.

    Definition:
    union employee  //union name
    {
        int employee_id;    //member1 int variable which,
                            //will store employee id
        
        char employee_name[20];   //member2, character array which will,
                                  //store name of employee.
        
        char employee_desig[15];   //member3,character array which will,
                                  //store designation of employee.
        
        int employee_salary;   //member4, int variable which will,
                                 //store employee salary.
        
        
    };
    

    • In Union employee , each member will share 20 bytes ,because it is the size of largest data of member employee_name




  • Q:Define a union Book and it should have book name,book id ,book price,and author name as an member in it.

    Definition:
    union Book  //union name
    {
        int Book_id; //member1 int variable which,
                        //will store Book id
        
        char Book_name[20];//member2, character array which will,
                              //store name of book.
        
        char author_name[30];//member3,character array which will,
                               //store name of author.
        
        int Book_price; //member4, int variable which will,
                            //store price of book.
        
        
    };
    

    • In Union Book , each member will share 30 bytes ,because it is the size of largest data of member author_name.




  • The union template ends with the semicolon ';'.
  • The members of union are enclosed in parenthesis {}.

  • Now we have learned how to define a union ,so let's use it.
  • For using it , we have to declare the union.
  • let's see syntax of declaring union variable
  • Syntax:
    union union_name  variable_name;
    

  • union keyword is used while declaring structure variable.
  • Above we have defined student data type,so declare a variable of student data type.

    Declaration:
    void main()
    {
        
     union student s1; //s1 is a variable of student type.
                       //as student is a  datatype  which is,
                         //define by us.
                
    }                     
    




  • Q:declare a variable 'emp1' of employee datatype.
    employee datatype has define above.

    Declaration:
    void main()
    {
        
     union employee emp1; //emp1 is a variable of employee type.
                       //as employee is a  datatype  which is,
                         //define by us.
                
    }                     
    




  • Q:declare a variable 'b1' of Book datatype.
    Book datatype has define above.

    Declaration:
    void main()
    {
        
     union Book b1; //b1 is a variable of Book data type.
                       //as Book is a  datatype  which is,
                         //define by us.
                
    }                     
    








  • Unions are very usefull , as they efficiently use the computer memory.
  • So , This is how we can make programmer defined data types.







  • Related Concept:



    People Also Searched: