`union` — C Keyword
`union` — C Keyword
The union keyword in C: defines a type where all members share the same storage.
`union` — C Keyword
The union keyword in C: defines a type where all members share the same storage.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
union (C)Defines a type where all members share the same memory. The size equals the size of the largest member. Only one member is active at a time.
union Name {
Type1 member1;
Type2 member2;
};
#include <stdio.h>
#include <stdint.h>
union Bytes32 {
uint32_t value;
uint8_t bytes[4];
};
int main(void) {
union Bytes32 u;
u.value = 0xDEADBEEF;
printf("%08X\n", u.value);
for (int i = 0; i < 4; ++i) {
printf("%02X ", u.bytes[i]); /* little-endian bytes */
}
printf("\n");
return 0;
}
union with an int tag field to track the active member.unionint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}