请教个c语言共用体问题

来源:百度知道 编辑:UC知道 时间:2024/06/07 02:29:29
请教高手帮忙解释下面2个问题
#include<stdio.h>
void main()
{union
{ short int i[2];long k;char c[4];}t,*s=&t;
s->i[0]=0x39;s->i[1]=0x38;
printf("%x\n",s->k);
}
求输出结果,答案是380039,我输入电脑后给出的结果是39
请教下这题目是怎么做的?
2
union u{ int k;char c[2];}u1;
main()
{
u1.c[0]=13;
u1.c[1]=0;
printf("%d\n",u1.k);
}
解释上说k占有2个字节,这2字节分别被赋值为0和13,那为什么只输出13??
谢谢

1、在tc下是39,在vc下是380039
2、因为输出的是k不是c[1]和c[2]

公用体不推荐这么用的吧,你给啥赋值就用啥,不要乱用。

看起来好难啊

好难啊

会不会跟cpu的Little endian还是Big endian有关呢?
Little endian就输出380039
Big endian就输出39
我这没有tc,仅仅作为猜测。