C Programming Union

In this tutorial, you will learn about the union in the C language. Unions are also the user-defined datatype similar to the structure in many ways. Both the unions and structures have a similar type of syntaxes. They differ from each other only in the context of memory usage. The members of the structure will have its own memory location., whereas all the members of the union will have a single memory location. The size of the union variable will always be the size of its largest element. Union offers an efficient way of using the same memory space for different purposes. Please read the details of the tutorial of the structure to make sure that you meet all the prerequisites. It will enhance your concept and help you to understand this topic well.

How can we define the union?

Defining the union statement is the same as that of defining the structure statement. The union will also consist of the elements of the different data types. We will use the union keyword for defining the unions.

union union_name
{
member definition;
member definition;
…………..
member definition;
}

Now, Let’s go through an example of a union. See the example below to understand the union more clearly.

union
union student
{
char name[50]
int roll;
}

How to create union variables?

At the time we define a union variable, it creates a user-defined type. As no memory is allocated until this time. So we need to create variables for allocating the memory for the given union type and work on it. You will learn to create the union variables in the illustration below.

The above process of declaring the variable can also be done in the following way:

In both cases above, two union variables stu1 and stu2 and a union pointer stu3 are created.

How to access union members?

We will use the member access operator(.) for accessing any member. We will use the keyword union for defining variables of union type. We will use . operator to access the normal variables and -> to access the pointer variables. The following program illustrates the use of union in our program.

Code:

Output:

The above code will display its output as below:

Enter the name of the student: jack
Enter the id of student: 3
Enter the address of the student: Boston
The name of the student entered is Boston
The id of the student entered is 1953722210
The address of the student entered is Boston
In the above code, we see that the first and the second variable in the union prints the garbage value and only the third variable prints the true value. As in union, different data types will share the same memory.  For this reason, the only variables whose value is currently stored will have the memory.
Now let’s look at the same example again. But this time we will print one variable at a time which is the main purpose of having unions.
The above code will generate its output as below.
Output:
Enter the name of the student: jack
The name of the student entered is jack
Enter the id of student: 3
The id of the student entered is 3
Enter the address of the student: Boston
The address of the student entered is Boston
Here the output infers that all the members of the union are printed well. As or union will use one variable at a time.
SHARE C Programming Union

You may also like...

Leave a Reply

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

Share