Typedef一个位域变量

我想要一个1位整数的typedef,所以我虽然这个typedef int:1 FLAG; 但是我遇到了错误,我有办法吗? 谢谢

没有。

C程序中最小的可寻址“事物”是字节char
char至少为8位长。
因此,您不能拥有少于8位的类型(或任何类型的对象)。

你可以做的是有一种类型,对象占用的位数至少与char一样多,并忽略大部分位

 #include  #include  struct OneBit { unsigned int value:1; }; typedef struct OneBit onebit; int main(void) { onebit x; x.value = 1; x.value++; printf("1 incremented is %u\n", x.value); printf("each object of type 'onebit' needs %d bytes (%d bits)\n", (int)sizeof x, CHAR_BIT * (int)sizeof x); return 0; } 

您可以在ideone上看到上面的代码。