S
sangeeta chowdhary
#include<stdio.h>
Hey friends ,I want to discuss this code
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d %d %d\n",u.ch[0],u.ch[1],u.i);
return 0;
}
Lets suppose int takes 4 bytes,and char takes 1 byte
According to me,4 bytes will be allocated for union,0th byte will be
for ch[0],1st byte will be for ch[1],and 0-3 bytes for int.
Now ch[0] holds- 00000011
and ch[1] holds- 00000010
and in this way u.i should have-00000011 00000010 00000000 00000000
But output of this code is- 3 2 515
Please tell me where I am going wrong?
Hey friends ,I want to discuss this code
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d %d %d\n",u.ch[0],u.ch[1],u.i);
return 0;
}
Lets suppose int takes 4 bytes,and char takes 1 byte
According to me,4 bytes will be allocated for union,0th byte will be
for ch[0],1st byte will be for ch[1],and 0-3 bytes for int.
Now ch[0] holds- 00000011
and ch[1] holds- 00000010
and in this way u.i should have-00000011 00000010 00000000 00000000
But output of this code is- 3 2 515
Please tell me where I am going wrong?