Study of union data-type. & Union definition.

#include <stdio.h>

struct xyz
{
int j;
char ch[2];
};

union myUnion
{
float f; /* 4 bytes */
int i[2]; /* 4 bytes */
char c[4]; /* 4 bytes */
struct xyz p; /* 4 bytes */
};

int main(void)
{
/* Declaring union variable */
union myUnion u;

printf( "Using u to store 1 float value:" );
printf( "\nEnter float : " );

scanf( "%f", &u.f );

printf( "\n u.f = %f Address : %p", u.f, &u.f );

printf( "\nUsing u to store 2 int values:" );

printf( "\nEnter 2 integers : " );

scanf( "%d %d", &u.i[0], &u.i[1] );

printf( "\n u.i[0] = %d Address : %p", u.i[0], &u.i[0] );

printf( "\nu.i[1] = %d Address : %p", u.i[1], &u.i[1] );

printf( "\nUsing u to store 4 char values:" );
/* H/W - write apprpriate statements.
You can use a loop for accessing chars */

printf( "\n Address of u : %p", &u );

printf( "\n Size of u : %u", sizeof(u) );

return 0;
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin