#include <iostream>
union Data {
int i;
float f;
char c;
};
int main() {
// Declare a variable of type Data
Data data;
// Assign a value to the integer member of the Data union
data.i = 5;
std::cout << "The value of the integer member is " << data.i << std::endl;
// Assign a value to the float member of the Data union
data.f = 3.14;
std::cout << "The value of the float member is " << data.f << std::endl;
// Assign a value to the char member of the Data union
data.c = 'A';
std::cout << "The value of the char member is " << data.c << std::endl;
return 0;
}
output :
The value of the integer member is 5
The value of the float member is 3.14
The value of the char member is A
0 Comments