#include <stdio.h>
// Define a union to represent either an integer or a float
union Value {
int i;
float f;
};
int main(void) {
// Declare a union variable and initialize it with an integer
union Value val;
val.i = 10;
// Print the value of the union as an integer
printf("val.i = %d\n", val.i);
// Modify the value of the union and print it as a float
val.f = 3.14;
printf("val.f = %f\n", val.f);
return 0;
}
This program defines a union called Value
that can represent either an integer or a float. It declares a union variable val
of type union Value
and initializes it with an integer. It then prints the value of the union as an integer and modifies it to be a float. When the value of the union is modified, the previous value is overwritten.
Here is an example of the output of this program:
val.i = 10
val.f = 3.140000
0 Comments