Unions In C Programming

Unions in c programming language; In this tutorial, i am going to show you what is union in c programming language and how to define and declare of union in c programming with the help of examples.

C Programming Unions

Here, i will show you what is union in c programming language and how to define and declare of union in c programming with the help of examples; is as follows:

  • What is union in C
  • Define a union in C
  • Declare a Union in C
  • Access members of a union
  • Example 1 – Union program for student details in C

What is union in C

Union in c programming is a collection of variables of different datatypes in the shared memory location.

Note that:- Unions and structures are conceptually similar. The basic difference is in terms of storage. In structure each member or variable has its own storage location, whereas all members of union uses a single shared memory location which is equal to the size of its largest data member or variable.

Declare a union in C

union is define using the union keyword; as shown below:

union union_name
{
member definition;
member definition;
…………..
member definition;
}

Declare a Union in C

You can use the following syntax to declare a union in c programming using the union keyword; as shown below:

union item
{
    int m;
    float x;
    char c;
}It1;

In the above declared union; it contains three members each with a different data type. However only one of them can be used at a time. This is due to the fact that only one location is allocated for all the union variables, irrespective of their size. The compiler allocates the storage that is large enough to hold the largest variable type in the union.

In the union declared above the member x requires 4 bytes which is largest amongst the members for a 16-bit machine. Other members of union will share the same memory address.

Access members of a union

Use the following syntax for accessing any union member and it is similar to accessing structure members; as shown below:

union test
{
    int a;
    float b;
    char c;
}t;

t.a;    //to access members of union t
t.b;     
t.c;

Example 1 – Union program for student details in C

#include <stdio.h>
union student
{
    char name[50];
    int id;
    char address[50];
};
int main( )
{
    union student stu;
     printf("\nEnter the name of the student: ");
    scanf("%s", &stu.name);
    printf("Enter the id of student: ");
    scanf("%ld", &stu.id);
    printf("Enter the address of the student: ");
    scanf("%s", &stu.address);
   
    
    printf("The name of the student entered is %s\n", stu.name);
    printf("The id of the student entered is %d\n", stu.id);
    printf("The address of the student entered is %s\n", stu.address);
    
    return 0;
}

Output of the above c program as shown below:

Enter the name of the student: tutsmake.com
Enter the id of student: 10
Enter the address of the student: London USA

The name of the student entered is London
The id of the student entered is 1684959052
The address of the student entered is London

In the above Union program for student details in C, you can see that the first and the second variable in the union prints the garbage value and only the third variable prints the true value. As in a union, different data types will share the same memory.  For this reason, the only variables whose value is currently stored will have the memory.

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.