<a href="#" title="C Standard Library ">C Standard Library <a href="#" title="C Standard Library ">C Standard Library <a href="#" title="C Standard Library ">C Standard Library <a href="#" title="C Standard Library ">C Standard Library <a href="#" title="C Standard Library ">C Standard Library <a href="#" title="C Standard Library ">C Standard Library
C Structures
C Bit Fields
Deep Dive
Scripting
Web Services
Computer Science
Programming Languages
Scripting Languages
Network Services
Development Tools
Programming
Web Design & Development
Software
C Unions
A union is a special data type that allows you to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.
Defining a Union
To define a union, you must use the union statement in a manner similar to defining a structure. The union statement defines a new data type with multiple members. The format of the union statement is as follows:
union {
member definition;
member definition;
...
member definition;
} ;
union tag is optional, and each member definition is a standard variable definition, such as int i; or float f; or any other valid variable definition. At the end of the union definition, before the final semicolon, you can specify one or more union variables, which is optional. Here is a way to define a union type named Data with members i, f, and str:
union Data {
int i;
float f;
char str;
} data;
Now, a variable of type Data can store an integer, a floating-point number, or a string of characters. This means that a single variable (the same memory location) can be used to store multiple types of data. You can use any built-in or user-defined data type inside a union based on your requirement.
The memory occupied by a union will be large enough to hold the largest member of the union. For example, in the above example, Data will occupy 20 bytes of memory space because the maximum size occupied by the string is 20 bytes. The following example will display the total memory size occupied by the above union:
Example
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str;
};
int main( ) {
union Data data;
printf( "Memory size occupied by data : %dn", sizeof(data));
return 0;
}
When the above code is compiled and executed, it produces the following result:
Memory size occupied by data : 20
Accessing Union Members
To access any member of a union, we use the member access operator (.) . The member access operator is coded as a period between the union variable name and the union member that we wish to access. You can use the union keyword to define variables of a union type. The following example shows the usage of a union:
Example
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str;
};
int main( ) {
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %dn", data.i);
printf( "data.f : %fn", data.f);
printf( "data.str : %sn", data.str);
return 0;
}
When the above code is compiled and executed, it produces the following result:
data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : C Programming
Here, we can see that the values of i and f members of the union get corrupted because the final value assigned to the variable has occupied the memory location, which is the reason that the value of str member is getting printed very well. Now let's look at the same example once again where we will use one variable at a time, which is the main purpose of having unions:
Example
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str;
};
int main( ) {
union Data data;
data.i = 10;
printf( "data.i : %dn", data.i);
data.f = 220.5;
printf( "data.f : %fn", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %sn", data.str);
return 0;
}
When the above code is compiled and executed, it produces the following result:
data.i : 10
data.f : 220.500000
data.str : C Programming
Here, all the members are getting printed very well because one member is being used at a time.
C Structures
C Bit Fields
iFlytek Xingchen Coding Plan includes free model call quotas for DeepSeek, GLM, Kimi, MiniMax, a one-stop experience and deployment platform. Configuration Guide Β₯3.9/month Activate Now
7 Notes Write a Note
#0 simone
183***1483@qq.com 73 Structures and Unions
The memory length occupied by a structure variable is an integer multiple of the largest field size within it (Reference: Calculation of Structure Size ).
The memory length occupied by a union variable is equal to the length of the longest member variable. For example, the union Data defined in the tutorial occupies 20 bytes each (because the char str variable occupies 20 bytes), not 4+4+20=28 bytes each.
union Data {
int i;
float f;
char str;
} data;
simone simone
183***1483@qq.com 9 years ago (2017-03-23)
#0 Wind Chasing Willows
295***031@qq.com
Reference Link 175
Purpose of Unions
To save memory. When there are two very long data structures that are not used simultaneously, such as one representing a teacher and one representing a student, using structures to track both teacher and student information would be somewhat wasteful! Using a union only requires the space occupied by the longest data structure, which is sufficient!
Union Application Scenarios
Unions are used in data packets in communication: because you don't know what kind of packet the other party will send, using a union makes it simple. You define several packet formats, and after receiving a packet, you can directly extract data based on the packet's format.
Wind Chasing Willows Wind Chasing Willows
295***031@qq.com
Reference Link 9 years ago (2017-05-23)
#0 sfvx
268***0313@qq.com 60
union Data {
int i;
float f;
char str;
double d;
} data;
The memory occupied by the union is not 9 chars, i.e., 9 bytes, but is double that of double, i.e., 16 bytes, and each time the output is the value closest to it before.
sfvx sfvx
268***0313@qq.com 9 years ago (2017-09-18)
#0 Dolphin Has the Sea, Kite Has the Wind
709***515@qq.com 36
When programming, it is often necessary to determine whether the machine is big-endian or little-endian. Using a union is very convenient for this:
union {
char str;
int dat