Summary: in this tutorial, we will introduce a new type called C union. A C union is similar to a structure that groups related data objects into a single variable. Unlike a structure, a union has a different memory allocation mechanism for its members.
What is a C union?
By definition, union is a type that allows you to store different data types in the same memory location, but not at the same time. A union is a group of data objects that share a single block of memory.
C union syntax
The syntax of union is similar to the syntax of constructing a struct. The following illustrates the syntax of union:
1 2 3 4 5 6 7 8 9 | union tag { type1 member; type2 member2; … } |
In the C union syntax above, to define a union:
- First, you start with the
unionkeyword. - Second, you define
tagfor the union. - Third, you specify all the members of the union with their types.
To access member of a union you use the ( .) operator. For example:
1 | tag.member |
C Union vs. Struct
In a structure, each member stores data separately. If you change the value of one member of a structure, other member’s values do not change.
However, in a union, all the members share the same block of memory. This block of memory is big enough to store the largest member. Smaller members use as much memory as necessary. If you change value of a member, other members’ values change as well.
If you need to store data in members simultaneously, you must use a structure.
Initialize a union
In ANSI C, there are two ways to initialize a union:
- You initialize a union to another union with the same type.
- Alternatively, you can initialize a union by initializing the first member of a union.
The following program demonstrates how to initialize a union in both ways.
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 | /* * File: main.c * Author: zentut.com * C union initialization */ #include <stdio.h> #include <stdlib.h> int main() { typedef union { char ch; int flag; float f; } data; data d; d.ch = 'A'; data d2 = d; // initialize one union to another data d3 = {'B'}; // initialize first member of union return 0; } |
C Union Example
In this example, we have an account structure that could be personal or business account based on the account_type enumeration. If it is a personal account the info member is associated with the person structure, otherwise it is associated with the company structure.
Just take few minutes go through the code to have a better understanding of using 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | /* * File: main.c * Author: zentut.com * C union example */ #include <stdio.h> #include <stdlib.h> enum account_type { personal = 1, business = 2 }; /* person name */ struct person { char* name; }; /* company with name and tax no*/ struct company { char* name; char* tax_no; }; typedef union { struct person individual; struct company biz; } profile; typedef struct { char* username; char* password; enum account_type type; profile info; } account; void display(account acc); int main() { printf("Union Demo\n"); account acc1, acc2; acc1.type = personal; acc1.username = "acc1"; acc1.password = "secret"; acc1.info.individual.name = "John Doe"; display(acc1); acc2.type = business; acc2.username = "acc2"; acc2.password = "secret2"; acc2.info.biz.name = "My Company"; acc2.info.biz.tax_no = "112121"; display(acc2); return 0; } /* displays account on screen */ void display(account acc) { switch(acc.type) { case personal: printf("Personal Account\n"); printf("username:%s\nname:%s\n",acc.username, acc.info.individual.name); break; case business: printf("Business Account\n"); printf("username:%s\ncompany:%s\ntax no.:%s\n",acc.username, acc.info.biz.name, acc.info.biz.tax_no); break; } printf("-------------------------------\n"); } |
In this tutorial, you have learned how to use C union and understand the differences between a union and a structure.