#includestdio.h
#includestring.h
int main()
{
typedef union
{
int a;
char b[10];
float c;
}
Union;
Union x,y = {100};
x.a = 50;
strcpy(x.b,hello);
x.c = 21.50;
printf(Union x : %d %s %f \n,x.a,x.b,x.c );
printf(Union y :%d %s%f \n,y.a,y.b,y.c);
return 0;
}
Someone pls explain
U should know that union's elements share memory alloted through the largest
data type like in this case 10 bytes is alloted and is shared by all the
union elements.
So in union x :- last element modified is c, so that's why it is printing
garbage values for int and char[] elements...
actually if
+1 to sagar.
@vartika: if you don't know the concept of Union then understanding this
program will be quite lenghty, because it will involve lot of calculation.
try to check some simple examples, like:
union a
{
int a;
char c;
};
int main()
{
union a xy;
xy.a=65;
printf(%c,xy.c);