Easy Tutorial
❮ C Function Atexit C Exercise Example83 ❯

Union

Union is a special data type that allows you to store different data types in the same memory location. You can define a union with multiple members, but only one member can contain a value at any given time. Unions provide an efficient way to use the same memory location for multiple purposes.

Defining a Union

To define a union, you must use the union statement, similar to how you define a structure. The union statement defines a new data type with multiple members. The format of the union statement is as follows:

union [union tag]
{
   member definition;
   member definition;
   ...
   member definition;
} [one or more union variables];

The union tag is optional, and each member definition is a standard variable definition, such as int i; or float f; or any other valid variable definition. At the end of the union definition, before the last semicolon, you can specify one or more union variables, which is optional. Below is a definition of a union type named Data with three members i, f, and str:

union Data
{
   int i;
   float f;
   char str[20];
} data;

Now, a variable of type Data can store an integer, a float, or a string. This means that a single variable (same memory location) can store multiple types of data. You can use any built-in or user-defined data types within a union as needed.

The memory occupied by a union should be large enough to store the largest member of the union. For example, in the above example, Data will occupy 20 bytes of memory space, as the string is the largest member. The following example will display the total memory size occupied by the union:

Example

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

union Data
{
   int i;
   float f;
   char str[20];
};

int main( )
{
   union Data data;        

   printf( "Memory size occupied by data : %d\n", sizeof(data));

   return 0;
}

When the above code is compiled and executed, it produces the following result:

Memory size occupied by data : 20

Accessing Union Members

To access the members of a union, we use the member access operator (.). The member access operator is a period between the union variable name and the union member we wish to access. You can use the union keyword to define variables of union type. The following example demonstrates the use of unions:

Example

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

union Data
{
   int i;
   float f;
   char str[20];
};

int main( )
{
   union Data data;        

   data.i = 10;
   data.f = 220.5;
   strcpy( data.str, "C Programming");

   printf( "data.i : %d\n", data.i);
   printf( "data.f : %f\n", data.f);
   printf( "data.str : %s\n", data.str);

   return 0;
}

When the above code is compiled and executed, it produces the following result:

data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : C Programming

Here, we can see that the values of the union members i and f are corrupted because the last assigned value occupies the memory location, which is why the str member is output correctly. Now let's look at the same example, but this time we use only one variable at a time, which demonstrates the main purpose of using unions:

Example

#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;
}

When the above code is compiled and executed, it produces the following result:

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

Here, all members are output correctly because only one member is used at a time.

❮ C Function Atexit C Exercise Example83 ❯