English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A union is a user-defined data type. All members of the union share the same memory location. The size of the union is determined by the size of the largest union member. It is best to use a union if the same memory location needs to be used for two or more members.
A union is similar to a structure. The creation method of a union variable is the same as that of a structure variable. The keyword 'union' is used to define a union in C language.
This is the syntax of union in C language.
union union_name { member definition; } union_variables;
Here,
union_name-Any name for the union.
member definition-member variable set.
union_variable-This is the union object.
This is a union example in C language.
#include <stdio.h> #include <string.h> union Data { int i; float f; }; data, data1; int main( ) { printf( "Memory size occupied by data : %d\t%d", sizeof(data), sizeof(data1)); return 0; }
Output result
Memory size occupied by data : 44
In the above program, a union object is used to create union data.
union Data { int i; float f; }; data, data1;