A union is a special data type available in C that allows the storage of different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.
Syntax : 
union [union_Name]
{
member definition;
member definition;
...
member definition;
};
Declaration of union
union student 
{ 
     char student_name[50]; 
     int student_id[50];     
}; 
Example : 
#include <stdio.h> 
#include <conio.h> 
#include<string.h>
union student 
{ 
char student_name[50];
Int student_id[50];
}; 
int main( ) 
{ 
union student std1;
strcpy(std1.student_name,"Ram");
Std1.student_id=12345;
Printf(“Student Name =%s”,student_name);
Printf(“Student ID =%d”,student_id);
return 0; 
} 
0 Comments