C Union

Like structure, union in c language is a user-defined data type. Unions work in the same way as structures except that all variables are contained in the same location in memory. Enough space is allocated for only the largest variable in the union. All other variables must share the same memory location. Unions are defined using the union keyword.

difference between structure and union
Note
The union can only store information in one field at one time. So a union can be viewed as a variable type that can contain many different variables as members (like a structure), but only actually holds one of them at a time (unlike a structure).

Advantage of union over structure

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

Disadvantage of union over structure

Only the last entered data can be stored in the union. It overwrites the data previously stored in the union.

Union Definition

The syntax for defining a Union and declaring a variable of Union is same as that of Structure; except the keyword used is union.

union <union-name>
{
<data-type> <variable-name>;
.
<data-type> <array-name> [<size>];
.
union <other-union-name>;
.
};
Note
A structure can have union as a data member and union can have a structure as a data member.

The following code snippet defines a union student

snippet
union student
{
int marks;
float percentage;
};

In the above example if it would have been a structure then the size of this structure would have been 6 bytes. However, as it is a union its size is 4 bytes (largest data member is float).

The following code snippet defines, declares and initializes a union student.

snippet
union student {
    int marks;
    float percentage;
} std1, std2 = {10};

/* Only one assignment is needed */

Let's see the example to define union for an employee in c.

snippet
union employee
{   int id;
    char name[50];
    float salary;
};

C Union example

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

snippet
#include 
#include 
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, "Sonoo Jaiswal");//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 : John Abraham

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

Accessing Union Members

To access union members we can use dot operator (.) between union variable and union member name as below.

<Union-Variable-Name>.<MemberAddress>

For example, to access marks of union student we can do as follows.

snippet
union student {
    int marks;
    float percentage;
} std1, std2 = {10};

union student std2;
std2.marks = 20;

Now, the data member percentage of union student also contains the same value 20 but in floating point format. When the other data member of union is assigned some value, the marks member is overwritten with that.

Also, two variables of same union type can be used on two sides of an assignment operator.

union_var1 = union_var2;
Nesting of Union

Similar to structure definition, Union can contain other Union variables as members . This is called Nesting of Union. We can use dot operator to access nested union and use dot operator again to access variables of nested union as is in case of structures. Also note that a union definition can’t contain the variable of its own type as a data member.

snippet
#include <stdio.h>
union student {
    int rno;
    char name[50];
}
std;
void main() {
    printf("\n\t Enter student roll no: ");
    scanf("%d", & std.rno);
    printf("\n\n\t Enter student name : ");
    scanf("%s", std.name);
    printf("\n\n Student roll no : %d",
        std.rno);
    printf("\n\n Student name : %s",
        std.name);
}
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +