# include <stdio.h>
void main()
{
union number
{
int n1;
float n2;
};
union number x;
printf("Enter the value of n1: ");
scanf("%d", &x.n1);
printf("Value of n1 = %d", x.n1);
printf("\nEnter the value of n2: ");
scanf("%f", &x.n2);
printf("Value of n2 = %f\n", x.n2);
}
OUTPUT:
Enter the value of n1: 10
Value of n1 = 10
Enter the value of n2: 50
Value of n2 = 50.000000
Sample C Program To Find The Size Of A Union.
# include <stdio.h>
void main()
{
union sample
{
int m;
float n;
char ch;
};
union sample u;
printf("The size of union = %d\n", sizeof(u));
/* initialization */
u.m = 25;
printf("%d %f %c\n", u.m, u.n, u.ch);
u.n = 0.2;
printf("%d %f %c\n", u.m, u.n, u.ch);
u.ch = 'p';
printf("%d %f %c\n", u.m, u.n, u.ch);
}
OUTPUT:
The size of union = 4
25 0.000000
1045220557 0.200000
1045220464 0.199999
Comments
Post a Comment
Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...
Experienced guys can share their resumes at admin@interview-made-easy.com