这是我的位域
struct {
unsigned char v64 : 1;
unsigned char leg : 7;
} valid;
然后我收到警告:
main.c:17:3: warning: type of bit-field ‘v64’ is a GCC extension [-pedantic]
main.c:18:3: warning: type of bit-field ‘leg’ is a GCC extension [-pedantic]
如果我更改为
int
则没有警告。但我想要一个字节的位域(无符号字符)。如何?
最佳答案
如果您不想收到警告,请删除 gcc
-pedantic
选项。
在 C99 中, gcc
使用 -pedantic
发出警告,但允许具有位域的实现定义类型(如 unsigned char
)。
在 C90 中,只允许 int
、 unsigned int
和 signed int
。
实际上,在 C90 和 C99 中,C 都不需要警告(它只是 C90 中的未定义行为,但 C 不需要未定义行为的警告)。警告由 gcc
添加,-pedantic
仅供引用。
关于c - 使用带无符号字符的位域时发出警告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10906238/