Union In C Programming Language

Union In C Programming Language

Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hello readers, welcome to our blog. In the last article, you had learnt about the Structures In C Programming Language. I hope you all understood how to use a structure in c, how to define structures and how to access structures member functions but in today’s article, you will learn how to declare a Union in c programming language.  So let’s start.

Introduction to Union in c programming language

C programming language provides you with a special data type. This data type is called Union In C Programming Language. With Union in c, you can store many types of data types in the same memory location. You can create many variables of different data types inside the unions. But at one time the value can be stored in the same variable. With Unions, you can use the same memory location in many ways.

Unions are similar to structures in c language. Just the difference is that every member of the structure occupies a separate memory location and the size of them all is different. But all members of the union use the same memory location and its size is the same as the largest size member.

Defining a Union in c programming language

To define the Union In C Programming Language, you use the union keyword. This is similar to defining the structure. The unions’ basic syntax is as follows.
union union_name{
    data_type var1;
    Ðata_type var2;
    ..
    ..
    data_type varn;
}u1.u2,….un;
Firstly you define union keyword and unique union name. After this, you define variables in curly brackets. After this, ending curly brackets make union variables by putting a comma.
union input
{
     int a;
     float b;
}in;
Now in that variables which have an input type can store an integer value or store a float value. You can not store the value in both variables together.

Accessing Union Member Variables

Union members also access the same way as structure members are accessed. First, you write the union name. After that. The name of the member is written by placing the dot (.) Operator.
You can assign the value to union members in this way.
in.a = 10;

If you want to print any union member, then you can do this as follows.

printf(“%d”,in.a);

 

A Complete Example of Union In C Programming Language

If you try to print all the members of the union together by assigning values together then the output will be an invalid show. When you assign the value to another variable after one, the value in memory will remain the same.

In simple words, the member who will be initialized in the last will be stored in the value memory.

#include <stdio.h>
union input
{
    int a;
   float b;
}in;
int main()
{
    in.a = 15;
    printf(“Value of a is : %d\n”,in.a);
    in.b = 20;

    printf(“Value of b is : %f”,in.b);

return 0;

}
In the above-given program, the value of the first a has been set and it has been printed. After this, the value of b variable is set and it has been printed. As I told you earlier, the value of the variable whose value will be set to the last one will be in the value union variable.
Therefore every time the value has been set first and the value of the old variable has been printed before setting another value of another member.
Value of a is :  15
Value of b is : 20.00000
I hope you understand how to use Union In C Programming Language. If you understand then please share my hard work with your friends. Subscribe my newsletter for upcoming article’s updates direct in your mailbox. If you have any confusion or query then you can leave a message on my facebook page. At last, I would like to say please don’t forget to subscribe to my youtube channel. Thank you for visiting.

Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *