Prev Next
C Union is also like structure, i.e. collection of different data types which are grouped together. Each element in a union is called member.
- Union and structure in C are same in concepts, except allocating memory for their members.
- Structure allocates storage space for all its members separately.
- Whereas, Union allocates one common storage space for all its members
- We can access only one member of union at a time. We can’t access all member values at the same time in union. But, structure can access all member values at the same time. This is because, Union allocates one common storage space for all its members. Where as Structure allocates storage space for all its members separately.
- Many union variables can be created in a program and memory will be allocated for each union variable separately.
- Below table will help you how to form a C union, declare a union, initializing and accessing the members of the union.
Using normal variable | Using pointer variable |
Syntax: union tag_name { data type var_name1; data type var_name2; data type var_name3; }; |
Syntax: union tag_name { data type var_name1; data type var_name2; data type var_name3; }; |
Example: union student { int mark; char name[10]; float average; }; |
Example: union student { int mark; char name[10]; float average; }; |
Declaring union using normal variable: union student report; |
Declaring union using pointer variable: union student *report, rep; |
Initializing union using normal variable: union student report = {100, “Mani”, 99.5}; |
Initializing union using pointer variable: union student rep = {100, “Mani”, 99.5}; report = &rep; |
Accessing union members using normal variable: report.mark; report.name; report.average; |
Accessing union members using pointer variable: report -> mark; report -> name; report -> average; |
Example program for C union:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <stdio.h> #include <string.h> union student { char name[20]; char subject[20]; float percentage; }; int main() { union student record1; union student record2; // assigning values to record1 union variable strcpy(record1.name, "Raju"); strcpy(record1.subject, "Maths"); record1.percentage = 86.50; printf("Union record1 values example\n"); printf(" Name : %s \n", record1.name); printf(" Subject : %s \n", record1.subject); printf(" Percentage : %f \n\n", record1.percentage); // assigning values to record2 union variable printf("Union record2 values example\n"); strcpy(record2.name, "Mani"); printf(" Name : %s \n", record2.name); strcpy(record2.subject, "Physics"); printf(" Subject : %s \n", record2.subject); record2.percentage = 99.50; printf(" Percentage : %f \n", record2.percentage); return 0; } |
Output:
Union record1 values example Name : Subject : Percentage : 86.500000; Union record2 values example Name : Mani Subject : Physics Percentage : 99.500000 |
Explanation for above C union program:
There are 2 union variables declared in this program to understand the difference in accessing values of union members.
Record1 union variable:
- “Raju” is assigned to union member “record1.name” . The memory location name is “record1.name” and the value stored in this location is “Raju”.
- Then, “Maths” is assigned to union member “record1.subject”. Now, memory location name is changed to “record1.subject” with the value “Maths” (Union can hold only one member at a time).
- Then, “86.50” is assigned to union member “record1.percentage”. Now, memory location name is changed to “record1.percentage” with value “86.50”.
- Like this, name and value of union member is replaced every time on the common storage space.
- So, we can always access only one union member for which value is assigned at last. We can’t access other member values.
- So, only “record1.percentage” value is displayed in output. “record1.name” and “record1.percentage” are empty.
Record2 union variable:
- If we want to access all member values using union, we have to access the member before assigning values to other members as shown in record2 union variable in this program.
- Each union members are accessed in record2 example immediately after assigning values to them.
- If we don’t access them before assigning values to other member, member name and value will be over written by other member as all members are using same memory.
- We can’t access all members in union at same time but structure can do that.
Example program – Another way of declaring C union:
In this program, union variable “record” is declared while declaring union itself as shown in the below program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> #include <string.h> union student { char name[20]; char subject[20]; float percentage; }record; int main() { strcpy(record.name, "Raju"); strcpy(record.subject, "Maths"); record.percentage = 86.50; printf(" Name : %s \n", record.name); printf(" Subject : %s \n", record.subject); printf(" Percentage : %f \n", record.percentage); return 0; } |
Output:
Name : Subject : Percentage : 86.500000 |
Note:
- We can access only one member of union at a time. We can’t access all member values at the same time in union.
- But, structure can access all member values at the same time. This is because, Union allocates one common storage space for all its members. Where as Structure allocates storage space for all its members separately.
Difference between structure and union in C:
C Structure
|
C Union
|
Structure allocates storage space for all its members separately. |
Union allocates one common storage space for all its members. Union finds that which of its member needs high storage space over other members and allocates that much space |
Structure occupies higher memory space. | Union occupies lower memory space over structure. |
We can access all members of structure at a time. | We can access only one member of union at a time. |
Structure example: struct student { int mark; char name[6]; double average; }; |
Union example: union student { int mark; char name[6]; double average; }; |
For above structure, memory allocation will be like below. int mark – 2B char name[6] – 6B double average – 8B Total memory allocation = 2+6+8 = 16 Bytes |
For above union, only 8 bytes of memory will be allocated since double data type will occupy maximum space of memory over other data types. Total memory allocation = 8 Bytes |