C Unions

1. What is the size of the given union?

union un {
int a;
int arr[20];
}

Ans: The size of the given union is 20 x 4 bytes = 80 bytes. Even if the array is a collection of similar data elements, it is considered to be a single entity by the C compiler.

2. Can we store data in multiple union members at the same time?

No. We can only store data in a single member at the same time. For example in the following C program, both x and y share the same location. If we change x, we can see the changes being reflected in y. 

C




// C program to check if we can store data in multiple union
// members
#include <stdio.h>
 
// Declaration of union is same as structures
union test {
    int x, y;
};
 
int main()
{
    // A union variable t
    union test t;
 
    t.x = 2; // t.y also gets value 2
    printf("After making x = 2:\n x = %d, y = %d\n\n", t.x,
           t.y);
 
    t.y = 10; // t.x is also updated to 10
    printf("After making y = 10:\n x = %d, y = %d\n\n", t.x,
           t.y);
    return 0;
}


Output

After making x = 2:
 x = 2, y = 2

After making y = 10:
 x = 10, y = 10

3. What are the applications of unions?

Unions can be useful in many situations where we want to use the same memory for two or more members. For example, suppose we want to implement a binary tree data structure where each leaf node has a double data value, while each internal node has pointers to two children, but no data. If we declare this as: 

C




struct NODE {
    struct NODE* left;
    struct NODE* right;
    double data;
};


then every node requires 16 bytes, with half the bytes wasted for each type of node. On the other hand, if we declare a node as the following, then we can save space. 

C




struct NODE {
    bool is_leaf;
    union {
        struct {
            struct NODE* left;
            struct NODE* right;
        } internal;
        double data;
    } info;
};




C Unions

The Union is a user-defined data type in C language that can contain elements of the different data types just like structure. But unlike structures, all the members in the C union are stored in the same memory location. Due to this, only one member can store data at the given instance.

 

Similar Reads

Syntax of Union in C

The syntax of the union in C can be divided into three steps which are as follows:...

Example of Union

C // C Program to demonstrate how to use union #include   // union template or declaration union un {     int member1;     char member2;     float member3; };   // driver code int main() {       // defining a union variable     union un var1;       // initializing the union member     var1.member1 = 15;       printf("The value stored in member1 = %d",            var1.member1);       return 0; }...

Size of Union

...

Difference between C Structure and C Union

The size of the union will always be equal to the size of the largest member of the array. All the less-sized elements can store the data in the same space without any overflow....

FAQs on C Unions

...