Union in C Programing with Example

Union in C Programing with Example

C Union is a derived data type which is very similar to the structure data type. But there is some difference between them is the allocation of memory.

The union is also a derived data type in C programming Language. It is a collection of different types of primitive data types. The only difference between Union and Structure is the allocation of memory. We can access the only members at a time in the union but in Structure multiple members are allowed.

All Members of the Union share the same memory location but in structure all members have separate memory locations.

Syntax of Union in C

[c]
union test
{
char name[60];
int rollno;
}t1,t2;
[/c]

Above Syntax is a union declaration syntax. We use “union” keyword and after that name of the union, in this case test is the name of union.

Here we have two data types char and int and two union variables t1,t2, they belong to test union.

Example 1: Union program in C to check the size

Below is the first example of a union program in c to see the size taken by the union data type.

[c]
#include <stdio.h>
#include <string.h>

union Test {
int i;
float f;
char str[40];
};

int main( ) {

union Test test;

printf( “Memory size occupied by test : %d\n”, sizeof(test));

return 0;
}
[/c]

In this case, the first memory was assigned to int i and then the same memory location was assigned to float f and at last same is assigned to str[40]. So the first two locations are overridden by char at last so that output is showing 40.

Example 2: Union program in C

Using the operator(.) you can access any member of a union. And union keyword is used to define the variables of union type. 

Let’s see the below example for better understandings.

[c]
#include <stdio.h>
#include <string.h>

union Test {
int i;
float f;
char str[40];
};

int main( ) {

union Test test;

test.i = 10;
test.f = 220.5;
strcpy( test.str, “C Programming”);

printf( “test.i : %d\n”, test.i);
printf( “test.f : %f\n”, test.f);
printf( “test.str : %s\n”, test.str);

return 0;
}
[/c]

Output:

union program in c

In the above output, you can see the value of int i and float f has changed. it is because its memory was overridden by char at last so char value is safe and printed correctly.

Example 3: Union Program in C

Now Let see this example where some modification is done in the previous example.

[c]
#include <stdio.h>
#include <string.h>

union Test {
int i;
float f;
char str[40];
};

int main( ) {
union Test test;
test.i = 10;
printf( “test.i : %d\n”, test.i);
test.f = 220.5;
printf( “test.f : %f\n”, test.f);
strcpy( test.str, “C union”);
printf( “test.str : %s\n”, test.str);
return 0;
}
[/c]

Output:

In this example you can see that we are able to print the correct value. It is only because before allocating the same int space to float we have printed the int value. Same as before allocating value to char we have printed the value of a float.

Now I hope you are able to understand the union in c language.

Thank you 🙂