English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C Language Unions (Union)

In this tutorial, you will learn about unions in C programming. Specifically, how to create unions (unions), access their members, and understand the differences between unions and structures. Unions are also known as unions and belong to the structure type in C language data types.

A union is a special data type that allows you to store different data types at the same memory location. You can define a union with multiple members, but only one member can have a value at any given time. The union provides an effective way to use the same memory location.

Union is also a user-defined type, similar to C'sstructThere is a difference in one keyword. The structure allocates enough space to store all its members, while the union allocates space only to store the largest member.

How to define a union?

We use the union keyword to define a union. Here is an example:

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

The above code defines the derived type union car.

Create union variable

After defining a union, it will create a user-defined type. However, no memory is allocated. To allocate memory for a given union type and use it, we need to create a variable.

This is how we create union variables.

union car
{
  char name[50];
  int price;
};
int main()
{
  union car car1, car2, *car3;
  return 0;
}

Another way to create a union variable is:

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

In both cases, a union car type union variable car is created1, car2and union pointer car3.

Accessing union members

We use the . operator to access union members. To access pointer variables, we also use-> operator.

In the above example

  • To access car1The price (price) of1.price.

  • To access car3The price (price) of* car3).price or car3-> price.

The difference between unions and structures

Let us take an example to illustrate the difference between unions and structures:

#include <stdio.h>
union unionJob
{
   //Define union
   char name[32];
   float salary;
   int workerNo;
}; uJob;
struct structJob
{
   char name[32];
   float salary;
   int workerNo;
}; sJob;
int main()
{
   printf("union size = %d bytes", sizeof(uJob));
   printf("\nstruct size = %d bytes", sizeof(sJob));
   return 0;
}

Output Result

union size = 32 bytes
structure size = 40 bytes

Why do the sizes of union variables and structure variables differ in this way?

In this case, the size of sJob is40 bytes, because

  • name[32The size of ] is32bytes

  • The size of salary is4bytes

  • The size of workerNo is4bytes

However, the size of uJob is32bytes. This is because the size of a union variable will always be the size of its largest element. In the above example, the largest element (name[32The size of ] is32bytes.

Using a union, all members shareThe same memory.

Example: accessing union members

#include <stdio.h>
union Job {
   float salary;
   int workerNo;
}; j;
int main() {
   j.salary = 12.3;
    //when j.workerNo is assigned a value
    // j.salary will no longer be retained12.3
   j.workerNo = 100;
   printf("Salary = %.01f\n", j.salary);
   printf("Number of Workers = %d", j.workerNo);
   return 0;
}

Output Result

Salary = 0.0
Number of Workers = 100