Union in C

Union in C Program with Examples

In this article, I am going to discuss Union in C Program with Examples. Please read our previous article where we discussed Structure in C Program.

Union in C:

A union is a collection of different types of data elements in a single entity. It is a collection of primitive and derived datatype variables. By using a union, we can create user-defined data type elements. The size of a union is max size of a member variable. In the implementation, for manipulation of the data, if we are using only one member then it is recommended to go for the union. When we are working with unions, all member variables will share the same memory location. By using union, when we are manipulating multiple members then actual data is lost.

The union is also a collection of dissimilar elements in contiguous memory locations, under a single name. They are user-defined datatypes. The name of the union (or the tag name) is treated as a data type, and the elements of the structure are known as its members. No memory is allocated during the definition of the union. Memory is only allocated when its variables are created (which is usually preceded by the keyword union). The variables of the union types occupy the memory size which is the maximum size among all its members. At one time simultaneously data can be stored in only one of its members. The members can be accessed by using the . (i.e. dot) operator.

The union is quite similar to the structures in C. The union is also a derived type of structure. A union can be defined in the same manner as structures just the keyword used in defining union in the union where keyword used in defining structure was struct.

Syntax:

Union Syntax in C

Example:

Union Example in C

Union variables can be created in a similar manner as structure variables.

Union in C with Examples

In both the cases, union variables c1, c2, and union pointer variable c3 of type union car is created.

Accessing members of a union

The member of unions can be accessed in a similar manner as that structure. Suppose, you want to access the price for union variable c1 in the above example, it can be accessed as c1.price. If you want to access the price for union pointer variable c3, it can be accessed as (*c3).price or as c3->price.

Note: All the properties of structures are applicable to a union like a variable, creation, pointer creation, array creation, typedef approach.

Program
#include <stdio.h>
#include <string.h>
union Data
{
    int i;
    float f;
    char str[20];
};

int main ()
{
    union Data data;
    data.i = 10;
    printf ("data.i : %d\n", data.i);

    data.f = 220.5;
    printf ("data.f : %f\n", data.f);

    strcpy (data.str, "C Programming");
    printf ("data.str : %s\n", data.str);

    return 0;
}

Output:

Accessing members of a union

Difference Between Structure and Union in C

Difference Between Structure and Union in C

In the next article, I am going to discuss Interview Questions and Answers in C language. Here, in this article, I try to explain the Union in C with Examples. I hope you enjoy this Union in C with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article

Leave a Reply

Your email address will not be published. Required fields are marked *