Union Types:
A union is a user-defined data type similar to a structure, except that all its
members share the same memory location. As a result, a union can store only one
value at a time, corresponding to one of its members.
The size of a union is determined by the size of its largest member. Unions are
used when a variable may hold values of different types at different times, and
memory efficiency is important.
A union is a user-defined data type similar to a structure, except that all its
members share the same memory location. As a result, a union can store only one
value at a time, corresponding to one of its members.
The size of a union is determined by the size of its largest member. Unions are
used when a variable may hold values of different types at different times, and
memory efficiency is important.
SYNTAX:
union union_name
{
datatype member1;
datatype member2;
};
EXAMPLE:
#include <stdio.h>
union Slot {
int id;
char grade;
};
int main() {
union Slot s;
s.id = 101;
printf("Value as ID: %d\n", s.id);
s.grade = 'A';
printf("Value as Grade: %c\n", s.grade);
return 0;
}