Professional Documents
Culture Documents
Union Is A User
Union Is A User
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.
Unions are similar to structures. Union variables are created in same manner as
structure variables. The keyword “union” is used to define unions in C++ language.
union union_name {
member definition;
} union_variables;
Here,
For example:
Union DMC
Int RNO;
Float percentage;
Char pass;
} Result;
Difference between structure and union:-
Union and structure are almost same but there is a difference. In structure different
variables are stored at different locations in memory, while in union ; different
variables are stored at the same space in memory.
union_name.member_name;
#include <conio.h>
union book{
int sr;
char name[20];
float price;
};
int main()
book vr;
cin>>vr.sr;
cout<<"Enter Book Name : ";
gets(vr.name);
cin>>vr.price;
return 0;
OUTPUT
Serial No.: 101
Record
A record is a data structure for storing a fixed number of elements. It is similar to a
structure in C++ language. At the time of compilation, its expressions are translated
to tuple expressions.
The keyword ‘record’ is used to create records specified with record name and its
fields. Its syntax is as follows −