#include <stdio.h>
union example {
int integer;
float floating_point;
char character;
};
int main() {
union example my_union;
my_union.integer = 42;
printf("Integer: %d\n", my_union.integer);
my_union.floating_point = 3.14;
printf("Floating point: %f %d\n", my_union.floating_point, my_union.integer);
my_union.character = 'A';
printf("Character: %c %d\n", my_union.character, my_union.integer);
//my_union.integer after assigning a character value to character
//leads to undefined behavior.
/*here after 3.14 value A will be stored in new location
In a union, padding may be present depending on the members within the
union. Padding is added by the compiler to ensure proper alignment and
memory access.*/
//so every time its taken new value old will get deleted or replaced
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: