A member of a structure or union may have any complete object type other than a variably modified type.123) In addition, a member may be declared to consist of a specified number of bits (including a sign bit, if any). Such a member is called a bit-field ;124) its width is preceded by a colon.
我是否理解正确,这表明 struct { int bit:1; 中的单个成员?
可能是一个符号位?
如果是这种情况,那么在某些实现中,这样的位字段可能存储的唯一值是 0
和 -0
,其中 -0
可能与 0
一旦存储或陷阱表示无法区分。
是否有任何实际的实现只能将一个值分配给这样的位域?
最佳答案
gcc 4.9.2 怎么样?
/* gcc -std=c11 -pedantic-errors -Wextra -Werror=all test.c */
#include <stdio.h>
#include <string.h>
struct foo {
int bit:1;
};
int main() {
struct foo f;
f.bit = 0;
f.bit = 1;
printf("%i\n", f.bit);
return 0;
}
编译它发出:
$ gcc -std=c11 -pedantic-errors -Wextra -Werror=all test.c
test.c: In function ‘main’: test.c:12:10: warning: overflow in
implicit constant conversion [-Woverflow]
f.bit = 1;
^
运行它发出:
$ ./a.out
-1
关于c - 是否有任何 C 实现具有无用的单位 `int` 位域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30584714/