gpt4 book ai didi

c - union 成员作为垃圾值(value)

转载 作者:行者123 更新时间:2023-11-30 21:16:29 25 4
gpt4 key购买 nike

#include <stdio.h>

int main()
{
union a
{
short int i;
char ch[2];
};

union a u;

u.ch[0]=3;
u.ch[1]=2;

printf("%d\n",u.ch[0]);
printf("%d\n",u.ch[1]);
printf("%d\n",u.i);
//printf("%d %d %d",u.ch[0],u.ch[1],u.i);
return 0;

}

给定代码的输出为 3,2,515 ,怎么会是 515 ?

最佳答案

您的机器是小端字节序,因此 MSB 为 2,LSB 为 3

2 * 256 + 3 = 515

关于c - union 成员作为垃圾值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com