CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C Programming – Understanding Unions in C

Unions are another useful types in C language, where all of it’s members starts at the same location in memory. That means, all members of the Union, have the same address; whereas, members in Structures have different addresses. This unique quality of the Unions, saves the memory usage.

Defining an Union in C

By using, union keyword, we can define an Union in C language. With the union, we can define our own user-defined data types. The definition of an union in C, is same as a structure definition. The syntax of an union, looks like below;

union [Union_Name]
{
	datatype_1 member_1;
	datatype_2 member_2;
	...
	...
	...
	datatype_N member_N;
} [union_variable_1, union_variable_2, ..., union_variable_N];

Union_Name or an identifier is an optional entry, which allows to create union variables of type, Union_Name. We can define union variables at the end of the Union definition. Members or elements of an Union of any data type, should enclose in the curly braces ‘{‘ and ‘}’. And Union definition must ends with a semicolon ‘;’.

Remember that, we cannot declare a static member within an Union.

Defining union variables, at the time of Union definition, is an optional. We can define them separately, like below;

union Union_Name union_variable_1 [, union_variable_2, ..., union_variable_N];

Access Union members or elements

Like structure elements, we can access Union members using, member access operator (.)Here is an example;

union_variable_1.member_1;

Once we can access Union members, we can modify the data like normal variables.

Initializing Union members

We cannot initialize the union members, with in the Union definition. If we do so, Compiler will throw an Error. We can initialize the members at the time of define the union variables. Here it looks like;

union_variable_1 = {value};

If you want to initialize specific union member, you can mention that during the initialization, like below; note that, you must use member access operator.

union_variable_1 = {.member_N = value};

Do you see any difference in initialization of Union members? Unlike structure members where we are allowed to initialize all members at a time; in Union members initialization, we actually initializing all members with a single value; because all members shares the same address space in memory, there is no meaning to specify all members during the initialization. If we do so, Compiler will throw below warning.

warning: excess elements in union initializer 

Confused? Let’s take a simple example, to clear this. Below is the table with Union members details;

31-1211-87-43-0Bit positionsUnion memberValue
0000101011001int (32-bits)int word;345
01011001char (8-bits)char byte;89

Observe that, the Union has 2 members; one member is of int type and another one is of char type. When we assign the value 345 to the union; it will store the value in the allocated memory. Here is the tricky thing will happen; as the memory is same to all the members of the union; when you access the int type member you will get the value 345; but, when you access char type member, from above example, you will get the value 89. Why? because, char type size is 8-bits (1-byte), you will get the value stored in the 1-byte of memory.

Similarly, when you modify the data for char type member, from above example, only that portion of memory will be updated.

I hope the above example, clarifies the way union works.

Structures within Unions

We can add almost any type of members to the Unions. We can add structures also as members to the Unions. Accessing structure members is same as accessing normal union members.

union_variable.structure_variable.member_1;

We can also add unions within unions.

Working Example

Let’s put all together, what we learned so far; and finally here is a working example;

// Malin

C Programming – Understanding Unions in C

Leave a Reply

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

Scroll to top