🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

C Unions

C Unions

Just like Structures, the union is a user-defined data type. All the members in unions share the same memory location. The union is a data type that allows different data belonging to different data types to be stored in the same memory locations. One of the advantages of using a union is that it provides an efficient way of reusing the memory location, as only one of its members can be accessed at a time. A union is used in the same way we declare and use a structure. The difference lies just in the way memory is allocated to their members.

Defining a Union

We use the union keyword to define the union.

The syntax for defining a union is,

union union_name
{
    //union_elements
} structure_variable;

Here’s one example of how a union is defined and used in main as a user-defined data type.

#include <stdio.h>

union Books
{
    char title[20];
    char author[100];
    float price;
    int pages;
};

int main()
{
    union Books book1;
    return 0;
}

Initialising and accessing union elements

Different from how we used to initialise a struct in one single statement, union elements are initialised one at a time.

And also, one can access only one union element at a time. Altering one union element disturbs the value stored in other union elements.

#include <stdio.h>
#include <string.h>

union Books
{
    char title[20];
    char author[100];
    float price;
    int pages;
};

int main()
{
    union Books book1;
    strcpy(book1.title, "C Programming");
    printf("%s\n", book1.title);

    strcpy(book1.author, "ABC");
    printf("%s\n", book1.author);

    book1.price = 123.99;
    printf("%f\n", book1.price);

    book1.pages = 300;
    printf("%d\n", book1.pages);

    return 0;
}

Output:

C Programming
ABC
123.989998
300

How are Structs and Unions similar?

  1. Structures and unions, both are user-defined data types used to store data of different types.
  2. The members of structures and unions can be objects of any type, including even other structures and unions or arrays.
  3. A union or a structure can be passed by value to functions and can be returned by value by functions.
  4. ‘.’ operator is used for accessing both union and structure members.

How are Structs and Unions different?

  1. The keyword union is used to define a union and a keyword struct is used to define the structure
  2. Within a structure, each member is allocated a unique storage area of location whereas memory allocated to a union is shared by individual members of the union.
  3. Individual members can be accessed at a time in structures whereas only one member can be accessed at a time in unions.
  4. Changing the value of one of the members of a structure will not affect the values of the other members of the structure, whereas changing the value of one of the members of a union will affect the values of other members in a union.
  5. Several members of a structure can be initialised at once, whereas only one member can be initialised in the union.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.