Error Handling in C
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.

Unlock memory-efficient coding in C—learn to define unions, manage shared member storage, compare with structs, and use unions for flexible type handling.
Unions in C are a data structure that allows you to store different data types in the same memory location. Unlike structures, where all members have their own separate memory locations, unions share the same memory location for all their members. This makes unions useful when you want to conserve memory and only need one of the members at a time. In this tutorial, we'll cover the basics of unions in C with examples.
Table of contents [Show]
To declare a union in C, you use the union keyword followed by the union name and a list of members enclosed in curly braces. Each member can have its own data type, and all members share the same memory location.
union MyUnion {
int i;
float f;
char c;
};
In this example, we've declared a union named MyUnion with three members: an integer i, a floating-point number f, and a character c. These members can be of different data types.
You can access the members of a union using the dot (.) operator. However, only one member can be accessed at a time, as they share the same memory space.
union MyUnion u;
u.i = 42;
printf("Value of u.i: %d\n", u.i); // Accessing the integer member
u.f = 3.14;
printf("Value of u.f: %f\n", u.f); // Accessing the float member
u.c = 'A';
printf("Value of u.c: %c\n", u.c); // Accessing the character member
In the code above, we create a union u and access its members i, f, and c one at a time.
The size of a union is determined by the size of its largest member. This is because all members share the same memory location, and the union needs to be large enough to accommodate the largest member.
You can use the sizeof operator to determine the size of a union:
union MyUnion {
int i;
float f;
char c;
};
printf("Size of MyUnion: %lu bytes\n", sizeof(union MyUnion));
You can initialize a union at the time of declaration just like other variables.
union MyUnion u = {0}; // Initializes all members to zero
Alternatively, you can initialize specific members:
union MyUnion u;
u.i = 42;
In C, unions are similar to structures (structs) in that they both allow you to group multiple variables together. However, the key difference is that in a structure, each member has its own memory location, while in a union, all members share the same memory location. Therefore, unions are used when you want to save memory and only need one member at a time.
Let's look at a few example programs to better understand unions in C:
#include
union TypeConverter {
int i;
float f;
};
int main() {
union TypeConverter tc;
tc.f = 3.14;
printf("Float value: %f\n", tc.f);
printf("Integer value (interpreted): %d\n", tc.i);
return 0;
}
In this example, we use a union to convert a floating-point number to an integer by simply assigning the float value to the union and then accessing the integer value. This demonstrates how unions can be useful for type conversion.
#include
union SaveMemory {
int i;
char c;
};
int main() {
union SaveMemory sm;
sm.i = 65;
printf("Integer value: %d\n", sm.i);
printf("Character value: %c\n", sm.c);
printf("Size of SaveMemory: %lu bytes\n", sizeof(union SaveMemory));
return 0;
}
In this example, we use a union to save memory. Even though we have two members (int and char), the size of the union is determined by the largest member (which is int), so it takes up 4 bytes of memory. However, we can access both the integer and character representations.
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.
Unlock flexibility in C—learn how to implement variadic functions using stdarg.h: handling ellipses (...), va_list, va_start, va_arg, va_end, with clear examples.
Unlock the power of C preprocessors — learn directives like #include, #define, macros, conditional compilation, and file inclusion for cleaner, flexible code.
These cookies are essential for the website to function properly.
These cookies help us understand how visitors interact with the website.
These cookies are used to deliver personalized advertisements.


