C Unions

Unionsis a special data type that allows you to store different data types in the same memory location. You can define a union with many members, but only one member can have a value at any time. Unions provide an efficient way to use the same memory location.

Defining a Union

To define a union, you must useunionstatement, in a similar way to defining 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];

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

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

Now,DataA variable of this type can store an integer, a floating-point number, or a string. This means that a variable (the same memory location) can store multiple data of different types. You can use any built-in or user-defined data type in a union as needed.

The memory occupied by a union should be sufficient to store the largest member of the union. For example, in the above instance, Data will occupy 20 bytes of memory space, because among the members, the string occupies the largest space. The following instance will show the total memory size occupied by the above 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 will produce the following results:

Memory size occupied by data : 20

Access union members

To access a member of a union, we useMember access operator (.). The member access operator is a period between the union variable name and the union member we want to access. You can useunionkeyword to define variables of union type. The following instance demonstrates the use of a union:

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 will produce the following results:

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

Here, we can see why the union'siandfthe member's value is corrupted, because the value last assigned to the variable occupies the memory location. This is alsostrmembers can be output completely and correctly. Now let's look at another identical instance. This time, we use only one variable at the same time, which also demonstrates the main purpose of using a union:

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 will produce the following results:

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

Here, all members can be output intact, because only one member is used at a time.

other extensions