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.
Contents
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.
{
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.
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.
union student { char name[50]; int id; }; int main() { union student stu1, stu2, *stu3; return 0; }
The above process of declaring the variable can also be done in the following way:
union student { char name[50]; int id; }stu1, stu2, *stu3;
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:
#include <stdio.h> union student { char name[50]; int id; char address[50]; }; int main( ) { union student stu; printf("\nEnter the name of the student: "); scanf("%s", &stu.name); printf("Enter the id of student: "); scanf("%ld", &stu.id); printf("Enter the address of the student: "); scanf("%s", &stu.address); printf("The name of the student entered is %s\n", stu.name); printf("The id of the student entered is %d\n", stu.id); printf("The address of the student entered is %s\n", stu.address); return 0; }
Output:
The above code will display its output as below:
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
#include <stdio.h> union student { char name[50]; int id; char address[50]; }; int main( ) { union student stu; printf("\nEnter the name of the student: "); scanf("%s", &stu.name); printf("The name of the student entered is %s\n", stu.name); printf("Enter the id of student: "); scanf("%ld", &stu.id); printf("The id of the student entered is %d\n", stu.id); printf("Enter the address of the student: "); scanf("%s", &stu.address); printf("The address of the student entered is %s\n", stu.address); return 0; }
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