Unions in C++

In a C++ programming language, the union is a special user-defined data type that allows to the storage of different data types in the same memory location.

“union”  uses the same memory location for multiple-purpose.

 Or

In a union data type, a single variable, i.e., the same memory location, can be used to store multiple types of data.

In a C++ programming union is similar to the structure but the major difference is that it requires less memory than structure.

Apart from this difference, you can define a union with many variables, but only one member can contain single value at any given time.

Defining Unions in C++

We can define a union with the “union” keyword. “union” defines a new user-defined data type with one or more than one members.

Syntax of union definition:

union  union_name {
   member definition;
   member definition;
   ………
   ………
} [one or more union variables];  

  In the union’s definition at the end & before the final semicolon, you can specify one or more union variables but it is optional.

When a union object is created, memory is allocated for the largest member of the union.

Changing the value of one member of the union will overwrite the values of all other members. It is the programmer’s responsibility to keep track of which member is currently in use.

Example:

union Book {
  int i;
  float f;
  char c[30];
}
book1, book2;

Above define a union type named book having three members i, f, and c.

In the above example, the variables of Book is book1 and book2. These variables can store an integer, a floating-point number, or a string of characters.

During the union definition, memory occupied by a union variable will be large enough to hold the largest member of the union.

In the above example, user-defined data type Book will occupy 30 bytes of memory space because this is the maximum space that can be occupied by a character string. 

Example: Write a program to store and print the following information such as name, roll number, class and section of the student using union.

#include 
using namespace std;
union Student {
  char name[30];
  char section[1];
  int rollno;
  int Class;
}
s1;
int main() {
  cout << "Enter the name, rollno, class & section of  student";
  cin >> s1.name;
  cin >> s1.rollno;
  cin >> s1.Class;
  cin >> s1.section;
  cout << "\n  Student details";
  cout << "\n Name =" << s1.name;
  cout << "\n Rollno =" << s1.rollno;
  cout << "\n Class = " << s1.Class;
  cout << "\n Section = " << s1.section;
  return 0;
}

Output

Enter the name, rollno, class & section of student
Aditya				// enter by user
23456				// enter by user
12				// enter by user
A				// enter by user
 Student details
 Name = A
 Rollno = 65
 Class = 65
 Section = A

In the above example, we can see that the values of name, rollno, and Class of union got corrupted because the final value assigned to the variable has occupied the memory location and this is the reason that the value of Section is getting printed very well.

Note: In a union, one variable can contain a single value at any given time which is the main purpose of having unions −

Correct Implementation of Above program

#include 
using namespace std;
union Student {
        char name[30];
        char section[1];
        int rollno;
        int Class;
}
s1;
int main() {
        cout << "Enter the name of  student";
        cin >> s1.name;
        cout << "\n Name =" << s1.name;
        cout << "\n Enter the rollno of  student";
        cin >> s1.rollno;
        cout << "\n Rollno =" << s1.rollno;
        cout << "\nEnter the class of  student";
        cin >> s1.Class;
        cout << "\n Class = " << s1.Class;
        cout << "\nEnter the section of  student";
        cin >> s1.section;
        cout << "\n Section = " << s1.section;
        return 0;
}

Output

Enter the name of  student
aditya						// enter by user
You enter  Name =aditya
 Enter the rollno of  student
9999						// enter by user
You enter  Rollno =9999
Enter the class of  student
12						// enter by user
 You enter Class = 12
Enter the section of  student
A						// enter by user
 You enter Section = A

Difference between structure and union

Lets see student structure

#include 
using namespace std;
struct Student_Structure {
        char name[30];
        char section[1];
        int rollno;
        int Class;
}
s1;
union Student {
        char name[30];
        char section[1];
        int rollno;
        int Class;
}
u1;
int main() {
        cout << "\n 1st difference is union occupies less memory than structure";
        cout << "\nsizeof structure : " << sizeof(s1);
        cout << "\n sizeof union : " << sizeof(u1);
        cout << "\n\n 2nd difference is union variable can store one value at a time & same memory location,   can be used to store multiple types of data.";
        // store student name, rollno, class & section using structure variable s1
        cout << "\n Enter the name, rollno, class & section of  student";
        cin >> s1.name;
        cin >> s1.rollno;
        cin >> s1.Class;
        cin >> s1.section;
        cout << "\n  Student details";
        cout << "\n Name =" << s1.name;
        cout << "\n Rollno =" << s1.rollno;
        cout << "\n Class = " << s1.Class;
        cout << "\n Section = " << s1.section;
        cout << "\n Enter the name, rollno, class & section of  student";
        cin >> u1.name;
        cin >> u1.rollno;
        cin >> u1.Class;
        cin >> u1.section;
        cout << "\n  Student details";
        cout << "\n Name =" << u1.name;
        cout << "\n Rollno =" << u1.rollno;
        cout << "\n Class = " << u1.Class;
        cout << "\n Section = " << u1.section;
        return 0;
}

Output

1st difference is union occupies less memory than structure
sizeof structure : 40
 sizeof union : 32

 2nd difference is union variable can store one value at a time & same memory location, can be used to store multiple types of data.
 Enter the name, rollno, class & section of  student
 Aditya				// enter by user
23456				// enter by user
12				// enter by user
A				// enter by user

  Student details
 Name =Aditya
 Rollno =23456
 Class = 12
 Section = A

 Enter the name, rollno, class & section of  student
Sam				// enter by user
9999				// enter by user
10				// enter by user
B				// enter by user

 Student details
 Name =B
 Rollno =66
 Class = 66
 Section = B

Here we discussed Unions in C++.

C++ Program For Student Details Using Array Of Objects

Categories C++