Union in C

In this blog, we will study union in c with a proper introduction of the union in c, the syntax of the union in c, an example of union followed by popper examples of union, accessing union in c member, advantages and disadvantages of union followed by some similarity and distinguishing feature between union and structure all with a proper explanation from scratch.

What is a Union in C

A union in c is a type of container that contains the inbuilt variables of different kinds and not only the available variables but also supports the user-defined variables. All the members of the union in c start at the same memory location.
An object’s union is a class whose data members are all mapped to the same location. In C, there is a unique data type called a union that enables the storing of many data kinds in the same memory address.

Syntax of the Union in C

We have to use a union keyword for declaring union followed by the name you wanna give to the container. Because enough bytes must be set aside to store the greatest-sized field, the union’s size is equal to the size of its largest field.

A collection of variable values and, optionally, a tag identifying the union are specified in a "union declaration." The "members" of the union, which are the variable values, might be of many sorts. In other languages, "variant records" are comparable to unions.

union phone
  int price;
  char name[50];

Above is the example of a general syntax of the union in c that you have to use the keyword named union after that the name of the container and in the container the values of its characteristics and properties.

Create Union Variables

We define a union in c outside the main function and to use a union in c inside the main function we have to create the user-defined variable here we will see the methods to create those user-defined methods as they are known as union variables.

union phone
  int price;
  char name[50];

int main()
  union phone phone1, phone2, *phone3;
  return 0;

In the above example we have created a union in c with the name of the phone which have two properties named price of the name and the name of the phone. In the main function, we have created the union variables by defining the anime of union first after that the name of the variables that we want to give to the variables we have given phone1 and phone 2 both ar union in c variables but phone 3 is union in c pointer.

We can create a union variable in an alternate way which is explained below:

union phone
  int price;
  char name[50];
}phone1, phone2, *phone3;

In the above example we have created union variables in an alternate way as here we have created them along the union in c declaration and not in the main function. Here we do not need to name the union and the name of the container again as we are declaring the union variable along the main union in the c declaration so there will be no need for a declaration. In this case also phone 1 and phone 2 are the union variables whereas phone 3 is a union in c pointer.

Example of Union in C

In this section, we will see the example of the union in c, in the example we will see the proper declaration of union in c with the initialization of union variables and the proper way of accessing union variables in the code.

Code Implementation:

#include <stdio.h>
#include <string.h>
union Example {
   int i;
   float f;
   char str[20];
int main( ) {

   union Example example;        

   example.i = 10;
   printf( "example.i : %d\n", example.i);
   example.f = 220.5;
   printf( "example.f : %f\n", example.f);
   strcpy( example.str, "C Programming");

   printf( "example.str : %s\n", example.str);

   return 0;


example.i : 10
example.f : 220.500000
example.str : C Programming

Explanation of the above code
In the above example we have created a union in c with the name Example and in that union in c we have an integer value, a float value, and a character array. Now in the main function, we have declared a union variable named example by following the syntax explained above and now we are initializing all the values of the union in c variable by just normal allocation of using the ‘.’ operator and you can see the output in the above image accordingly.

Advantages of Union in C

  • It occupies less memory or space compared to the structure
  • We can use union in c when we have the same memory location for more than one data member.
  • It will only take the space of the largest data member.
  • With the help of union in c you can only access the last variable directly.

Disadvantages of the Union in C

  • There is a cap with the union that at a time you can only use one union member.
  • Sometimes the storage space becomes a problem as it allies the common storage for all its members.
  • We cannot assign all the union variables at the same time.

Similarities between Structure and Union

There are many similarities between union in c and structures and in this part we will see some of them.

  1. Both union and structure are used to store different types of data in a single unit and they are user-defined data types.
  2. Functions may accept a structure or union as a parameter and return it as a parameter. The type of the argument and the function parameter must match. Similar to a scalar variable, a structure or union is supplied by value as the equivalent parameter.
  3. Only the assignment = and sizeof operators are supported by unions and structures. The members and member types of the two structures or unions in the assignment must match.
  4. To access member variables inside of both user-defined datatypes, use the ‘.’ operator, which has one of the highest precedences.
  5. Any object, including other unions, structures, or arrays, may be one of their members. A bit field can also be a part of a member.

Difference between Union and Structure in C

Parameters Union Structure
keyword We use the keyword union to define union. We use the keyword structure to define the structure.
Memory Memory is shared by each individual Each member is allocated different memory.
Accessing Members In union in c we can only access one member at a time In this we can access individual members at a time
Size Allocates with the size of the largest variable Allocates with the aggregate of the size of all the variables.
Value Altering Altering the value of one member will affect the value of another variable Altering the value of one member will not affect the value of another variable.
Initialization of members We can only initialize the first member of the union in c We can initialize the value of many variables at the same time.

In this article we have studied unions in c followed by a brief introduction to unions then a detailed explanation. We have also discussed the union in c syntax, and how to declare data members in the union. With an example of union, we have shown the proper use of data members with their initialization followed by the advantages and disadvantages of union and at last the similarity and distinguishing features between union and structure.

Leave a Reply

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