Latest web development tutorials

C union

Union is a special type of data, allowing you to a different location in the same type of data stored in memory.You can define a union with more members, but any time there can be only one member with the value. Unions provide a way to use the same memory location in an effective way.

Definition of union

To define a union, you must useunion statements, and define the structure in a similar manner.union statement defines a new data type, with a plurality of members. Union statement format is as follows:

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

union tag is optional, every member definition is the standard definition of a variable, such as int i; or float f; or other valid variable definitions.Before the end of the common body defined final semicolon, you can specify one or more of the union variables, which is optional. The following definition of a common body type named Data, there are three members i, f and str:

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

Now, the variableData type can store an integer, a float, or a string.This means that a variable (the same memory location) can store a plurality of various types of data. You can use any built-in or user-defined data types in a common body as needed.

Union should be enough storage memory occupied by the largest member of the union. For example, in the example above, Data will occupy 20 bytes of memory space, because each member, the space occupied by the string is greatest. The following example will show the above union occupied the total memory size:

#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 results:

Memory size occupied by data : 20

Access union member

In order to access to the common member countries, we usethe member access operator (.).Member access operator is a period variable names and union members of our union to be accessed between. You can use the keyword to define variablesunion union type.The following example demonstrates the use of a union:

#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 results:

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

Here we can see the values ofi and funion members are damaged, because the values assigned to the variables of the last occupied memory location, which is the reasonstrmembers to intact output. Now let's look at one and the same instance, this time we use only one variable at the same time, it also demonstrates the main purpose of using a union:

#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 results:

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

Here, all the members can output intact, because at the same time uses only one member.