C Programming Union

C Programming Union

In this chapter you will learn about unions in C programming. More specifically, learn how to create, access, and differentiate between unions and structures .

C Programming Union

In C programming, unions are completely similar to structs . Unions are derived data types like structures.

Defining a union in C programming is very easy and is exactly like a structure. Instead of the struct keyword, only the union keyword needs to be used.

				
					union car
{
  char name[50];
  int price;
};
				
			

How to create union variable?

Union variables are created in the same way that structure variables are created.

				
					 union car
{
  char name[50];
  int price;
} car1, car2, *car3;
				
			

or

				
					union car
{
  char name[50];
  int price;
};

int main()
{
  union car car1, car2, *car3;
  return 0;
}
				
			

In both cases union variables union carof data type car1 and car2 and car3 union pointer variable are created.

Accessing members of the union

The members of a union are accessed in the same way that the members of a structure are accessed.

In the above example if you want to access the member price of the union variable car then follow the procedure below:

				
					car1.price

				
			

Similarly if you want to access the member price of the union pointer variable car3 then follow the procedure below:

				
					(*car3).price
or;
*car3->price
				
			

Difference Between Union and Structure

 

Although unions and structures are similar in many ways, it is important to understand the difference between them.

Let us try to understand the basic difference between union and structure with the help of following example:

				
					#include <stdio.h>
union unionJob
{
//Union definition
char name[32];
float salary;
int workerNo;
} uJob;

struct structJob
{
//Structure definition
char name[32];
float salary;
int workerNo;
} sJob;

int main()
{
printf("size of union = %d", sizeof(uJob));
printf("\nsize of structure = %d", sizeof(sJob));
return 0;
}
				
			

output

				
					size of union = 32
size of structure = 40
				
			

Structures require more memory allocation than unions

The above example shows the difference in memory allocation between unions and structures.

In the case of structure variables, memory allocation equal to the sum of all members has to be kept.

Union variables on the other hand require memory equal to the maximum element of the union. .

Only one union member can be accessed at a time

Structure: All its members can be accessed simultaneously.

Union: Only one of its members can be accessed at a time and all other members contain garbage values.

				
					#include <stdio.h>
union job
{
   char name[32];
   float salary;
   int workerNo;
} job1;

int main()
{
   printf("Enter name:\n");
   scanf("%s", &job1.name);

   printf("Enter salary: \n");
   scanf("%f", &job1.salary);

   printf("Displaying\nName :%s\n", job1.name);
   printf("Salary: %.1f", job1.salary);

   return 0;
}
				
			

output

				
					Enter name
Tamim
Enter salary
12345.25
Displaying
Name: Trm$y   
Salary: 12345.2
				
			

Note: You can get different garbage values for names.

In the above program Tamim is stored job1.namefirst   and all other members of job1 like salary, workerNo will contain garbage values.

But when user enters the value of salary job1.salaryit stores 1234.23 and other members like name, workerNo will contain garbage value.

This way salary will be printed correctly in the output but name will display some random string.

Traversing unions through functions

 

Similarly if you want to access the member price of the union pointer variable car3 then follow the procedure below:

Leave a Comment

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