Home > Common Problem > body text

How to use union in c language

百草
Release: 2023-09-27 11:00:29
Original
2560 people have browsed it

The usage of union in C language is a special data type that allows different data types to be stored in the same memory location. The use of union can help us save memory space and can easily store different data types. Convert between types. When using union, you need to note that the corresponding member is valid and only one member can be accessed at the same time.

How to use union in c language

Union in C language is a special data type that allows different data types to be stored in the same memory location. The use of union can help us save memory space and make it easy to convert between different data types.

1. Define union:

In C language, we can use the keyword union to define a union type. A union is defined similarly to a structure, but all members in the union share the same memory space. The syntax of union is as follows:

   union union_name {
       member_type member1;
       member_type member2;
       // ...
   };
Copy after login

Among them, union_name is the name of union, member_type is the data type of member, member1, member2, etc. are members of union.

Example:

   union Data {
       int i;
       float f;
       char str[20];
   };
Copy after login

2. Access union members:

We can use the dot operator (.) to access the members of the union. Since the members of a union share the same memory space, only one member can be accessed at the same time. The syntax for accessing union members is as follows:

   union_name.member_name
Copy after login

Example:

   union Data data;
   data.i = 10;
   printf("data.i = %d\n", data.i);
   data.f = 3.14;
   printf("data.f = %f\n", data.f);
   strcpy(data.str, "Hello");
   printf("data.str = %s\n", data.str);
Copy after login

In the above example, we define a union named Data and create a variable named data. We can access different members of union through data.i, data.f and data.str.

3. The size of the union:

The size of the union depends on the size of its largest member. Since all members of a union share the same memory space, the size of the union can be large enough to accommodate the largest member.

Example:

   union Data {
       int i;
       float f;
       char str[20];
   };
   printf("sizeof(union Data) = %lu\n", sizeof(union Data));
Copy after login

In the above example, we use the sizeof operator to get the size of union Data. Since the size of float type is usually larger than the size of int and char arrays, the size of union Data will be equal to the size of float.

4. Application of union:

Union is often used in the following situations:

- Save memory space: When only one of multiple members will be used, you can Use union to save memory space.

- Type conversion: You can use union to convert between different data types, such as converting integers to floating point numbers, converting floating point numbers to character arrays, etc.

Example:

   union Convert {
       int i;
       float f;
   };
   union Convert convert;
   convert.i = 10;
   printf("convert.i = %d\n", convert.i);
   printf("convert.f = %f\n", convert.f);
   convert.f = 3.14;
   printf("convert.i = %d\n", convert.i);
   printf("convert.f = %f\n", convert.f);
Copy after login

In the above example, we define a union named Convert, which contains a member i of type int and a member f of type float. By modifying one member of the union, we can obtain the corresponding value in another member, thus achieving type conversion.

It should be noted that you need to be careful when using union, because members of union share the same memory space. After modifying the value of one member, the values ​​of other members will be overwritten. Therefore, when using union, you need to ensure that the corresponding member is valid and only one member can be accessed at the same time.

In summary, union in C language is a special data type that allows different data types to be stored in the same memory location. By defining a union and accessing its members, we can save memory space and convert between different data types. When using union, you need to note that the corresponding member is valid and only one member can be accessed at the same time.

The above is the detailed content of How to use union in c language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!