//test.c
#include <stdio.h>
struct node {
int flag:1;
int other:31;
};
int main(void)
{
struct node n;
n.flag = 1;
if (1 == n.flag) {
printf("n.flag is 1\n");
}
else {
printf("n.flag not 1, is %d\n", n.flag);
}
return 0;
}
$ gcc test.c -o t
$ ./t
n.flag not 1, is -1
结构体中一个位域的表示整数时
有符号的,取值范围0,-1
无符号的,取值范围0,1
//test2.c
#include <stdio.h>
struct node {
unsigned int flag:1;
int other:31;
};
int main(void)
{
struct node n;
n.flag = 1;
if (1 == n.flag) {
printf("n.flag is 1\n");
}
else {
printf("n.flag not 1, is %d\n", n.flag);
}
return 0;
}
$ gcc test2.c -o t2
$ ./t2
n.flag is 1