C Programming Tutorial
About Lesson

C Union

In this tutorial, we will learn how to create unions, access its members and learn the differences between unions and structures in C programming with the help of examples.


Union

A union is a user-defined data type used to store different type data types which are grouped together.

Each element/datatype in a union is called a member.

Union allocates one common storage space for all its members.

Note : Union occupies less memory because it occupies the size of the largest member only.


Syntax

union unionName {
  dataType member1;
  dataType member2;
  dataType member3;
  ...
  ...
  ...
};

Example

union teacher {
  char name[20];
  int ID;
  int age;
};

Here, we have define a union teacher.


How to Create union variables?

when a union is defined no memory is allocated and we cannot work with the union.
To allocate memory and work with unions we have to create union variables.

union Teacher {
  char name[20];
  int ID;
};
int main() {
  union Teacher teacher1, teacher2, *teacher3;
  return 0;
}

Another way of creating union variables

union Teacher {
  char name[20];
  int ID;
} teacher1, teacher2, *teacher3;

In both cases of the above cases, we have create union variables teacher1teacher2 and a union pointer *teacher3 of union Teacher type.


How to Access members of a union?

To access the members of a union we can use . dot operator.

To access the pointer variables, we use also use the -> operator.


Example: Accessing Union Members

#include <stdio.h>
union Teacher {
  int ID;
  float salary;
} teacher1, teacher2;

int main() {
  teacher1.ID = 56;
  teacher1.salary = 8000;
  printf("Teacher1 ID = %d\n", teacher1.ID);
  printf("Teacher1 salary = %f\n", teacher1.salary);
  teacher2.ID = 56;
  printf("Teacher2 ID = %d\n", teacher2.ID);
  teacher2.salary = 8000;
  printf("Teacher2 salary = %f\n", teacher2.salary);
  return 0;
}

Output

ID = 1174011904
salary = 8000.000000

Here, we have observed that the teacher1.id is not printed corrected but teacher1.salaryteacher2.ID and teacher2.salary printed correctly.

Note : In union we can only access the last assigned member and the previous data of union variable will erased when the other member is assigned.

When variable teacher1.salary is assigned the previous assigned value of teacher1.ID is vanished or overwritten with the value of teacher2.salary.


Difference between unions and structures

Structure Union
Structure allocates memory for all its members separately. Union allocates common memory for all its members which is the same as the member who needs high storage space.
It occupies more memory space. It occupies less memory space compares to structures.
We can access all the members of structure at a time. We can access only one member of union at a time.

Example : memory allocation difference of structure and union

#include <stdio.h>
union TeacherUnion {
  int ID1;
  float salary1;
} teacher1;

struct TeacherStruct {
  int ID2;
  float salary2;
} teacher2;

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

Output

size of union = 4 bytes
size of structure = 8 bytes

Why this difference in the size of union and structure variables?

Here, the size of teacher1 is 4 bytes because

  • the size of ID1 is 4 bytes
  • the size of salary1 is 4 bytes

Here, the size of teacher2 is 8 bytes because

  • the size of ID1 is 4 bytes
  • the size of salary1 is 4 bytes

Here, the size of union is less than structure because members of union shares the same memory allocation which is same as the member with largest size.

error: Content is protected !!