Union in C

Introduction

Same as structure and enum, the union is also a user-defined datatype. Different types of data can be stored using unions. It is declared and used in the way as structure.

There is a difference between structure and enum, which is very important to understand but before going to that part, let’s see how to define a union.

Defining a union

Syntax:

union union_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
};

 

In the above syntax, we use the ‘union’ keyword along with the union name. After that, various union members with their data types are defined in its body and enclosed in curly braces.

The above-written syntax is the same as the syntax of structure with a little difference in using the keyword ‘union’ in place of ‘structure’.

For example:

union student
{
char name[30];
int roll_no;
float marks;
};

 

Here, we use ‘student’ as the name of the union. name, roll_no, marks are its members with data-type char, int, float, respectively.

Declaration of the union variable

There are two methods to declare the variables for the union. We had already discussed both the methods in structure, and here we are going to study them again with the help of examples.

Method 1

union student
{
char name[30];
int roll_no;
float marks;
};
main()
{
union student s1, s2, s3;
}

 

Firstly, we define a union named ‘structure’ with its members of different data types.

In the ‘main’ function we declare three variables that are s1, s2, and s3 of type ‘union student’. Here ‘union student’ is working as a data type for these three variables.

Moreover, these three variables s1, s2, and s3 represent three different students of the union named ‘student’.

Method 2

union student
{
char name[30];
int roll_no;
float marks;
} s1, s2, s3;

 

This method declares the variables at the time of defining the union. Now, there is no need to declare the variables in the ‘main’ function.

Assigning values to variables of union

We can assign values to the union variables in the same way we do in structure.

strcpy(s1.name, “Abhishek”);

s1.roll_no=1903119;

In the first statement, the ‘s1’ variable access the member ‘name’, and using the ‘strcpy’ function we assign a string value to s1.name.

In the second statement, the ‘s1’ variable access the member ‘roll_no’ and an integer value is assigned to it.

Let’s write a Program the same as we did in structure and see what happens with the output.

Program:

#include<stdio.h>
#include<string.h>
union student
{
char name[20];
int age;
float weight;
float height;
};
int main()
{
printf("Details of the student are as below:\n");
union student s1;
strcpy(s1.name,"Thomson");
s1.age=21;
s1.weight=70;
s1.height=175.0;
printf("Name:%s\n",s1.name);
printf("Age:%d\n",s1.age);
printf("Height:%.2f\n",s1.height);
printf("Weight:%.2f\n",s1.weight);
return 0;
}

 

Output:

The above Program is well explained in the structure chapter. We just change the keyword ‘structure’ and write ‘union’ in its place.

The output we get from this approach is not accurate.

  • Why ‘s1.name’ doesn’t print name?
  • Why ‘s1.age’ is giving a random value of age as output?

Till now, we are not aware of the answers to the questions written above but the next part of this chapter will make it clear in-depth.

Memory allocated to Union in C

We can access only one member of the union at a time because there is only one memory block allocated to the union of the size which is equal to the maximum size of its member. As a result, we can use only one union at a time.

Rest all the other members will contain some garbage value.

Program:

#include<stdio.h>
#include<string.h>
union student
{
int roll_no;
char name[30];
int phone;
};
int main()
{
union student S;
S.roll_no=1903119;
printf("Roll no=%d\n",S.roll_no);
}

 

Output:

Now what happens in the above Program is, there are 3 members of the union and the member with a maximum size of 30 is ‘name’. Thus, a memory block of 30 bytes will be allocated to this union.

S.roll_no=1903119; by writing this statement, we are accessing the member ‘roll_no’ of size 4 bytes. This size is less than 30 bytes thus, the Program will work fine and it will store the value which is assigned to ‘S.roll_no’.

This means we have used 4 bytes of memory out of 30 bytes.

Understanding Garbage Value in C with respect to Union

Program:

#include<stdio.h>
#include<string.h>
union student
{
int roll_no;
char name[30];
int phone;
};
int main()
{
union student S;
S.roll_no=1903119;
S.phone=9888855200;
strcpy(S.name,"Sonali");
printf("Roll no=%d\n",S.roll_no);
printf("Phone no=%d\n",S.phone);
printf("Name=%s\n",S.name);
}

 

Output:

In the output of the above Program, the name got printed as it is but there is a garbage value at the place of ‘roll no’ and ‘phone no’.

It is clear to all of us that the memory allocated to union is 30 bytes i.e., the size of the member ‘name’ occupying the highest space.

Since we can access only one member at a time, that’s why the members get corrupted.

When we wrote S.roll_no=1903119; member ‘roll_no’ gets accessed and assigned with a value ‘1903119’.

After that, S.phone=9888855200; member ‘phone’ get accessed and assigned with a value ‘9888855200’.

In the last when we write the statement strcpy(S.name,”Sonali”); the string value ‘Sonali’ is assigned to member ‘name’. This results in assigning garbage values to the other two members, which are ‘roll_no’ and ‘phone’.

This is the reason why we get garbage values of ‘roll_no’ and ‘phone’, and the value of ‘name’ is printed as it is.

Program:

#include<stdio.h>
#include<string.h>
union student
{
int roll_no;
char name[30];
int phone;
};
int main()
{
union student S;
S.roll_no=1903119;
printf("Roll no=%d\n",S.roll_no);


S.phone=2222;
printf("Phone no=%d\n",S.phone);
strcpy(S.name,"Sonali");
printf("Name=%s\n",S.name);
}

 

Output:

Now the question arises, how do we get the same values assigned to the members?

This happens because we first assign a value to member ‘roll_no’ and then print it. By that time, the values of the other two members are garbage values.

Similarly, when S.phone=2222; is executed, the value is assigned to ‘phone,’ and there are some garbage values assigned to other members.

Finally, we printed ‘Sonali’ as the value of member ‘name’ because all other members now have some garbage values.

Program:

#include<stdio.h>
#include<string.h>
union student
{
int roll_no;
char name[30];
int phone;
};
int main()
{
union student S;
S.roll_no=1903119;
printf("Roll no=%d\n",S.roll_no);


S.phone=2222;
printf("Phone no=%d\n",S.phone);
strcpy(S.name,"Sonali");
printf("Name=%s\n",S.name);



printf("\nRoll no=%d\n",S.roll_no);
printf("Phone no=%d\n",S.phone);
printf("Name=%s\n",S.name);


}

 

Output:

The above Program is the same as the previous one. We just add the ‘printf’ statements again, in the end, to make it clear that the member ‘name’ contains its actual value and the other members have some garbage values.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.