Union is a user-defined data type . All the members of unions share same memory location . Size of union is decide by the size of largest member of Union . if you want to use same as memory location for two or more members , union is the best for that .
Unions are similiar to structure . unions variables are created in same manner as structure variables. The keyword "Unions" is used to define unions in C language .
Here is a syntax of Union in C
Union Union_name{
member definition ;
} Union variables ;
Example of Union :
// C++ program to demonstrate the
// making of structure
#include <bits/stdc++.h>
using namespace std;
// Define structure
struct GFG {
int G1;
char G2;
float G3;
};
// Driver Code
int main()
{
// Declaring a Structure
struct GFG abuzar;
abuzar.G1 = 85;
abuzar.G2 = 'G';
abuzar.G3 = 989.45;
cout << "The value is : "
<< abuzar.G1 << endl;
cout << "The value is : "
<< abuzar.G2 << endl;
cout << "The value is : "
<< abuzar.G3 << endl;
return 0;
}
Output :
The value is : 85
The value is : G
The value is : 989.45
0 Comments
Do not Comment and any spam text