Advertisement

Union in C++ with Examples || Code with abuzar

  

In this tutorials we will Learning about Unions and How union work in c++





union in c++

 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


Post a Comment

0 Comments