C Unions
In this page:
union UnionName {
data_type member1;
data_type member2;
};
union UnionName variable_name;
variable_name.member1 = value;
What is a Union?
This shared-memory design makes unions much more memory-efficient than structures when you only ever need one of several possible values at a time -- for example, representing a value that could be an int, a float, or a string, but never more than one simultaneously.
Example: What is a Union?
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Data {
int i;
float f;
};
// Program execution starts in main()
int main() {
union Data d;
d.i = 5;
// Print formatted text to the screen
printf("%d", d.i);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Defining a Union
This is syntactically identical to defining a structure, but the compiler's memory layout is fundamentally different: every member starts at the same base address rather than being laid out sequentially.
Example: Defining a Union
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Value {
int i;
char c;
};
// Program execution starts in main()
int main() {
union Value v;
v.i = 65;
// Print formatted text to the screen
printf("%d", v.i);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Memory Allocation in Unions
If your union has an int (4 bytes) and a double (8 bytes), the union's total size is 8 bytes -- exactly enough for its largest member, since all members overlap the same memory region rather than being stacked.
Example: Memory Allocation in Unions
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Data {
int i;
double d;
};
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%zu", sizeof(union Data));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Accessing Union Members
Writing to one member and then reading a different member reinterprets the same raw bytes as a different type, which is sometimes done deliberately (called 'type punning') but is usually a bug if done accidentally.
Example: Accessing Union Members
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Data {
int i;
float f;
};
// Program execution starts in main()
int main() {
union Data d;
d.i = 10;
d.f = 3.5f;
// Print formatted text to the screen
printf("%.1f", d.f);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Unions vs. Structures
Choose a structure when you need to store several related values simultaneously (like a point's x and y), and a union when you need to represent one value that could be several different types depending on context (like a variant/tagged data type).
Example: Unions vs. Structures
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point { int x, y; };
union Value { int i; float f; };
// Program execution starts in main()
int main() {
struct Point p = {1, 2};
union Value v;
v.i = 5;
// Print formatted text to the screen
printf("%d %d %d", p.x, p.y, v.i);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Reading a union member other than the one last written, which gives unreliable results.
- Assuming all union members hold values at once.
- Assuming a union's size is the sum of its members, when it is the size of the largest.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: