Hi all!!
See the following code:
#include <stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=1;
u.ch[1]=0;
printf("%d %d %d\n",u.ch[0],u.ch[1],u.i);
getch();
return 0;
}
The output that I get is:
1 0 2130509825
Why am I getting this number?
What I think the answer should be and why:
u.ch[0]=1
and u.ch[1]=0
means that the memory looks like-
10000000 00000000 00000000 00000000
As I am using windows, which is little endian (Am I wrong here?). This means that the field u.i
should be 2^0=1, and not 2130509825.
Thanks in advance !!