#include<stdio.h>
union logic
{
int i;
char x[2];
//char y;
}emb={510,’A’,’B’};
int main()
{
printf(“%d\n”,emb.i);
printf(“%c\n”,emb.x[0]);
printf(“%c\n”,emb.x[1]);
printf(“%d\n”,emb.i);
}
#include<stdio.h>
union logic
{
int i;
char x[2];
//char y;
}emb={510,’A’,’B’};
int main()
{
printf(“%d\n”,emb.i);
printf(“%c\n”,emb.x[0]);
printf(“%c\n”,emb.x[1]);
printf(“%d\n”,emb.i);
}
Union is like a chunk of memory that is used to store variables of different types. Once a new value is assigned to a field, the existing data is wiped over with the new data.
as far as i know you have to initialize each value separately
#include
#include
union logic
{
int i;
int x;
char j[12];
}emb;
int main()
{
emb.i=10;
// emb.x=12;
printf(“value is %d \n”,*(&(emb.i)));
emb.x=12;
printf(“address of i is %p and x is %p \n”,&(emb.i),&(emb.x));
printf(“value is %d \n”,*(&(emb.x)));
strcpy(emb.j,”hello world”);
printf(“%s\n”,emb.j);
printf(“address is %p \n”,&(emb.j));
return 0;
}
but ask SIR about this concept
~