Union
A union is a special data type and that allows to store different data types in the same memory location. We can define a union with many members but at given point of time only one member can contain a value. Union provide efficient way of using the same memory location for multiple purpose. The union keyword is used to define the union.
Let’s see the syntax to define union
union union_name { data_type member1; data_type member2; . . data_type memeberN; };
Let’s see the example to define union for an employee
union employee { int id; char name[50]; float salary; };
Let’s see the example of union
#include <stdio.h> #include <string.h> union employee { int id; char name[50]; }e1; int main( ) { e1.id=101; strcpy(e1.name, "Neha Jaiswal"); 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 : Neha Jaiswal
Submit your review | |
The Technical Funda
Average rating: 0 reviews