这是一台X86机器,操作系统为Linux 2.6红帽版。 以下是我的代码:
#include "stdio.h"
typedef struct ch_t
{
int c0:1;
int c1:1;
int c2:1;
int c3:1;
int c4:1;
int c5:1;
int c6:1;
int c7:1;
} ch;
typedef union chh_u
{
char a;
ch chat;
} chh;
int main(void)
{
chh uu;
uu.a = 6;
printf("\n%d", uu.chat.c0);
printf("\n%d", uu.chat.c1);
printf("\n%d", uu.chat.c2);
printf("\n%d", uu.chat.c3);
printf("\n%d", uu.chat.c4);
printf("\n%d", uu.chat.c5);
printf("\n%d", uu.chat.c6);
printf("\n%d", uu.chat.c7);
printf("\n%d\n", uu.a);
return 0;
}
正如我所预期的,输出应该是:
0 0 0 0 0 1 1 0 6
但实际输出为:
0 -1 -1 0 0 0 0 0 6
我不明白为什么会有以上的输出。
我认为6位顺序是0000 0110,在内存中,按照我的理解,位顺序也应该是0000 0110。但是输出结果显示不同。
有人可以解释一下吗?
something_t
。名称以_t
结尾的类型是保留给操作系统及其头文件使用的。 - fuz%d
是signed int
的正确格式说明符。你可能需要重新编写你的评论。 - fuz