0 votes
in C Plus Plus by

What is union? Explain with example?

1 Answer

0 votes
by

A union is a user-defined data type that allows users to store multiple types of data in a single unit. However, a union does not occupy the sum of the memory of all members. It holds the memory of the largest member only. Since the union allocates one common space for all the members we can access only a single variable at a time. The union can be useful in many situations where we want to use the same memory for two or more members.

Syntax:

union name_of_union
{
   data_type name;
   data_type name;
};

Related questions

0 votes
asked Jan 12 in C Plus Plus by GeorgeBell
0 votes
asked Jan 12 in C Plus Plus by GeorgeBell
...