How To Use Union Data Types in C++ With Example

How To Use Union Data Types in C++ With Example
How To Use Union Data Types in C++ With Example
 

How to use union data types

Union: A union is a special data type available in C++ Library that allows storing different data types in the same memory location. 

It 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 purposes.

Definition: A union is a collection of different data types. union is a user-defined data type.
Data types can be used like int, float, char, etc. union can access the member of the union by making the variable of structure.
Union creates a use union Keywords.


Syntax of Union

union (union_name)

{

Data_Type variable 1;

Data_Type variable 2;

Data_Type variable 3;

Data_Type variable 4;


};


Example Syntax of Union

union student

{

int id;

char name[100];

int marks;

int roll;

};


How To Declare of Union in C++?

union student

{

int id;

char name[100];

float marks;

int roll;

};

int main()

{

union student s;

return 0;

}

A union name is optional and each member definition is a normal variable definition.
such as int id; or float marks; or any other valid variable definition. 
At the end of the union's definition, before the final semicolon.

we can specify one or more union variables but it is optional.
Now, a variable of Data type can store an integer a floating-point number, or a string of characters. 

It means a single variable is stored same memory location we can be used to store multiple types of data.

We can use any built-in or user-defined data types inside a union based on your requirement.
 

How To Declare size_of  Union in C++?

#include<iostream>

#include<string.h>

using namespace std;

union student

{

int id;

char name[100];

float marks;

int rollno;

};

int main()

{

union student s;

cout<<"Memory Size:"<<sizeof(s));

return 0;

}


*****OUTPUT*****

Memory Size:100


Access Union Data Member: A union 
To access any data member of a union.
It can be used to access the member dot operator (.).


How To Acess Data Member of Union in C++?

#include<iostream>

#include<string.h>

using namespace std;

union student

{

int id;

char name[100];

float marks;

int rollno;

};


int main()

{

union student s;

s.id=807714;

strcpy(s.name,"C Language");

s.marks=78.67;

s.rollno=627722;


cout<<"Student Id:"<<s.id<<endl;

cout<<"Student Name:"<<s.name<<endl;

cout<<"Student Marks:"<<s.marks<<endl;

cout<<"Student Roll Number:"<<s.rollno<<endl;

return 0;

}

*****OUTPUT*****

Student Id:garbage value

Student Name:garbage value

Student Marks:garbage value 

Student Roll Number:627722


Write C program to create the union of 
two different books and display the same details

#include<iostream>

#include<string.h>

using namespace std;

struct Book 


int book_id; 

int book_price; 

char book_name[100]; 

char book_author[100]; 

}; 

int main() 


struct Book b1,b2; 

b1.book_id=807714; 

b1.book_price=450;  

strcpy(b1.book_name,"C Language");

strcpy(b1.book_author,"Ravi Kumar"); 

b2.book_id=4547714; 

b2.book_price=360; 

strcpy(b2.book_name,"Python Language"); 

strcpy(b2.book_author,"Rohan Dass");

cout<<"Book1 Id:"
<<b1.book_id<<endl;
 
cout<<"Book1 Price:"<<b1.book_price<<endl; 

cout<<"Book1 Name:"<<b1.book_name<<endl; 

cout<<"Book1 Auther:"<<b1.book_author<<endl;

cout<<"Book2 Id:"<<b2.book_id<<endl; 

cout<<"Book2 Price:"<<b2.book_price<<endl; 

cout<<"Book2 Name:"<<b2.book_name<<endl; 

cout<<"Book2 Auther:"<<b2.book_author<<endl;

return 0; 

}   

*****OUTPUT*****

Book1 Id:1769365842 

Book1 Price:1769365842 

Book1 Name: Ravi Kumar 

Book1 Auther Name: Ravi Kumar
 
Book2 Id:1634234194

Book2 Price:1634234194

Book2 Name: Rohan Dass 

Book2 Auther Name:Rohan Dass 


How To Use Union Data Type in C++? 

#include<iostream>

#include<string.h>

using namespace std;

union BookData 


int book_id; 

int book_price; 

char book_name[100]; 

char book_author[100]; 

}; 

int main() 


 union BookData bd; 

bd.book_id=807714; 

bd.book_price=450; 

strcpy(bd.book_name,"C Programming Language"); 

strcpy(bd.book_author,"Icoderweb");

cout<<"Book Id:"<<bd.book_id<<endl; 

cout<<"Book Price:"<<bd.book_price<<endl; 

cout<<"Book Name:"<<bd.book_name<<endl; 

cout<<"Book Auther:"<<bd.book_author<<endl; 

return 0; 

   
*****OUTPUT*****

Book Id:1685021513 

Book Price:1685021513 

Book Name:Icoderweb 

Book Auther Name:Icoderweb 


Enumeration: A Enumeration is a collection of named integer constants. Enumeration is user-defined data types.
it can be an enum Keyword used to create enumeration.

Syntax of Enumeration 

enum enum_name{value1,value2,value3...value n};


How To Use Enumeration in C++?

#include<stdio.h> 

enum days{

Sunday, Monday, Tuesday, Wednesday

,thrusday,friday,saturday}; 

int main() 


enum days d; 

d=friday; 

printf("Value of Friday:%d",d); 


*****OUTPUT*****

Value of Friday:5

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.