Union in C Programming | Union in C Language | Union in C
In this article union in c programming we give the information about union in c, define union, difference between structure and union and unions definition.
Union in C Programming:
- Union is a special data type by which we store different data types in the same memory location.
- In other words, “A union is a data type defined by the user. In this all the members share the same memory location.
- We can define union with many elements and each element in it is called member.
- It is defined by the union keyword.
- Unions are similar like structure but there is a difference between them. Structure provides memory space to store all the members whereas union provides memory to store only the largest member.
- We can access only one member of the union at a time. Not all members can be accessed at a time.
The union keyword is used to define it. The way we define the structure, we do it in the same way.
Below you have been given its syntax:-
1st data_type member;
2nd data_type member;
nth data_type member;
Advantage of union:-
Its benefits are as follows:-
1. It takes less memory than Structure, that is, it saves memory.
2. When we use union, only its last member can be directly accessed.
3. Using this we can store two or more data members in the same memory location.
4. It holds the data of only one data member.
int main( )
union demo1 t;
t.a = 100;
t.b = 300.2;
t.ch = ‘y’;
Accessing Union Members:-
To access any member of the union, we use the member access operator (.). It is also called dot (.) operator. We also use arrow (->) operator to access pointer variables.
Union in C Programming:
Its program is given below:-
union demo d1;
d1.a = 66;
// d2 is a pointer to union d1
union demo* d2 = &d1;
// Accessing union members using pointer
printf(“%d %c”, d2->a, d2->b);
Difference between structure and union:-
Unions are similar like structure but there is a difference between them. Structure provides memory space to store all the members whereas union provides memory to store only the largest member. We can access only one member of the union at a time.
The struct keyword is used to define the structure.
In this, each member is given a unique memory space.
If the value of any one data member is changed in the structure, then it does not affect the other data members.
In this, more than one member can be accessed at a time.
It supports flexible array.
The union keyword is used to define a union.
In this, all the members share the same memory.
If the value of one data member is changed in the union, then the value of other data members will also change.
In this, only one member can be accessed at a time.
It does not support flexible array.
- Object Oriented Programming Using C++ |BCA Semester II |History of C++
- Core Java Programming | BCA Part III | SEM-VI | What is Java
- Computer Fundamentals Notes | Computer Fundamentals Tutorial
- DOT NET Technology Tutorial | ASP.NET Tutorial for Beginners