Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Union

Union is very much similar to structure.
Union is a derived data type.
It contains the group of variables having same or different data types.

Syntax

union <tagname>
{
    data_type variable1;
    data_type variable2;
    data_type variable3;
        :
        :
    data_type variableN;
};

C Programming Language Union SyntaxFor example

union Employee
{
    int id_no;
    char name[30];
    float salary;
};

The main contrast between structure & union is that, structure reserves space in memory for all its members whereas union reserves space in memory for only one member that is larger in size as shown below.
In union, same space is used/shared by every member of the union.

Example

C Programming Language what is the difference between Structure and Union in MemorySince union shares the single reserved space for all its members, only one member can be accessed at a time.
Every member of the union has the same address. But every member of the structure has a different address.

Example

#include <stdio.h>

struct Employee
{
    int id_no;
    char name[30];
    float salary;
};

union Manager
{
    int id_no;
    char name[30];
    float salary;
};

struct Employee emp = {10,"Robin",44556.65};
union Manager mgr = {20,"Jack",98473.43};

int main()
{
    printf("\n sizeof(emp) = %d",sizeof(emp));
    printf("\n sizeof(mgr) = %d",sizeof(mgr));
    printf("\n");
    printf("\n Address of structure id_no = %d",&emp.id_no);
    printf("\n Address of structure name = %d",&emp.name);
    printf("\n Address of structure salary = %d",&emp.salary);
    printf("\n");
    printf(" Address of union id_no = %d",&mgr.id_no);
    printf("\n Address of union name = %d",&mgr.name);
    printf("\n Address of union salary = %d",&mgr.salary);	
    return 0;
} 

Output

sizeof(emp) = 40
sizeof(mgr) = 32

Address of structure id_no = 4206624
Address of structure name = 4206628
Address of structure salary = 4206660

Address of union id_no = 4206688
Address of union name = 4206688
Address of union salary = 4206688

Note

In the above output, the size of structure & union may vary by some amount due to holes present in them.

Members of the union are accessed exactly similar to structure members.

Syntax

union_variable.member;

Example

#include <stdio.h>

union Employee
{
    int id_no;
    float salary;
    char category;
};

int main()
{
    union Employee emp1;
	
    emp1.id_no = 25;
    printf("\n id_no of emp1 = %d",emp1.id_no);
    emp1.salary = 45455.55;
    printf("\n salary of emp1 = %f",emp1.salary);
    emp1.category = 'B';
    printf("\n Category of emp1 = %c",emp1.category);
	
return 0;
}

Output

id_no of emp1 = 25
salary of emp1 = 45455.550781
Category of emp1 = B

We cannot access union members together

If we tried to access more than one union members together, we might get the wrong output.

Example

#include <stdio.h>

union Employee
{
    int id_no;
    float salary;
    int team_no;
    char category;
};

int main()
{
    union Employee emp1;
	
    emp1.id_no = 35;
    emp1.salary = 55455.55;
    emp1.team_no = 4;
    emp1.category = 'B';

    printf("\n id_no of emp1 = %d",emp1.id_no);
    printf("\n salary of emp1 = %f",emp1.salary);
    printf("\n team number of emp1 = %d",emp1.team_no);
    printf("\n Category of emp1 = %c",emp1.category);

    return 0;
}

Output

id_no of emp1 = 66
salary of emp1 = 0.000000
team number of emp1 = 66
Category of emp1 = B

Initialization of union members during declaration

Only first member of the union can be initialized during declaration.

Example

#include <stdio.h>

union Employee
{
    int id_no;
    float salary;
    char category;
}emp1 = {20};

int main()
{
    printf("\n ID no of employee emp1 = %d",emp1.id_no);
    printf("\n Salary of employee emp1 = %f",emp1.salary);
    printf("\n Category of employee emp1 = %c",emp1.category);
	
    return 0;
}

Output

ID no of employee emp1 = 20
Salary of employee emp1 = 0.000000
Category of employee emp1 = ¶

 

Leave a reply