Output both value in a union : Union « Data Type « C Tutorial






#include <stdio.h>

union number {
   int x;
   double y;
};

int main()
{
   union number value; 

   value.x = 100; 
   printf( "%s\n%s\n%s%d\n%s%f\n\n",
      "Put a value in the integer member",
      "and print both members.",
      "int:   ", value.x,
      "double:\n", value.y );

   value.y = 100.0;
   printf( "%s\n%s\n%s%d\n%s%f\n",
      "Put a value in the floating member",
      "and print both members.",
      "int:   ", value.x,
      "double:\n", value.y );
   return 0;
}
Put a value in the integer member
and print both members.
int:   100
double:
0.000000

Put a value in the floating member
and print both members.
int:   0
double:
100.000000








2.26.Union
2.26.1.Union
2.26.2.Output both value in a union