/* 浮動小数点数の表現 */
#include <stdio.h>
typedef union cif
{
char c[4];
int i;
float f;
} UCIF, *Ucif;
/*
UCIF は union cif 型
Ucif は union cif 型へのポインタ
*/
/* void prnum( union cif *up ) と書いても良いが... */
void prnum( Ucif up )
{
int i;
printf("i = 0x%08x\n", up->i );
printf("c = ");
for( i = 0; i < 4; i++ )
{
if( i )
printf(", ");
printf("0x%02x", up->c[i] & 0xff );
}
printf("\n");
printf("i = %d\n", up->i );
printf("f = %f\n", up->f );
}
main()
{
UCIF ucif;
printf("Input an integer: ");
scanf("%d", &ucif.i );
prnum( &ucif );
printf("Input a float: ");
scanf("%f", &ucif.f );
prnum( &ucif );
printf("Input a hex: ");
scanf("%x", &ucif.i );
prnum( &ucif );
}
/* end of union.c */