Home >Backend Development >C++ >What does union mean in c language
Union is a data type in C language that can be used to save memory by allowing different types of data to be stored in the same memory location. It is used by declaring a structure containing members of different types, which share the same memory location, so that only one member's data can be stored at a time.
union in C
UNION is a C keyword that allows you to Store different types of data.
Function:
Syntax:
<code class="c">union union_name { data_type1 member1; data_type2 member2; ... };</code>
Among them:
is the name of the union.
,
data_type2, etc. are members of union, and they can have different data types.
Usage:
) to access members of the union, for example:
union_name.member1.
Example:
<code class="c">union my_union { int integer; float floating_point; char character; }; my_union my_data; my_data.integer = 10; printf("Integer value: %d\n", my_data.integer); my_data.floating_point = 3.14; printf("Floating-point value: %f\n", my_data.floating_point);</code>In the above example,
my_union is a union containing integer, floating point and character members. We first store an integer and then a float. Since the members of a union share the same memory location, the value of a floating point number will overwrite the value of an integer.
The above is the detailed content of What does union mean in c language. For more information, please follow other related articles on the PHP Chinese website!