Unions In C++
Union is a user-defined
datatype. All the members of union share same memory location. Size of union is
decided by the size of largest member of union. If you want to use same memory
location for two or more members, union is the best for that.Keyword is
“union” is use in union when we make program of union.
Syntax:
union union name
{
union memeber1;
union memeber2;
union memeber3;
};<union variable>;
Example:
union MCA
{
int age;
char name[20];
}p;
Program:
#include<iostream.h>
#include<conio.h>
union MCA
{
char name [30];
char place[50];
int pincode;
}p;
void main()
{
cout<<”input name”;
cin>>p.name;
cout<<”input place”;
cin>>p.place;
cout<<”input pincode”;
cin>>p.pincode;
cout<<p.name;
cout<<p.place;
cout<<p.pincode;
getch();
}
Output:
Input name Pramod
Input place Delhi
Input pinocde 201301
Pramod
Delhi
201301
No comments:
Post a Comment