#include<stdio.h>
int main()
{
union emp;
union emp{
int age;
char name[2];
};
union emp e1={512};
printf("%d,%d,%d",e1.age,e1.name[0],e1.name[1]);
return 0;
}
Here ive tried to craete a union and initialize its 1st member i.e "int age". As per my knowledge ANSI C compilers support this. My question is why i am getting an output like "512,0,2". If i replace 512 with 513, 514 and 768 i get the following oytputs. "513,1,2", "514,2,2", "768,0,3", Now i can see that the e1.name[0] is storing (the number)%256 and e1.name[1] is storing (the number)/256. It would be greatly appreciated if it is explained that why and how this happens. Thank you all.