使用带有unsigned char的bitfield时发出警告

Fab*_*cio 7 c

这是我的位域

struct {
    unsigned char v64 : 1;
    unsigned char leg : 7;
} valid;
Run Code Online (Sandbox Code Playgroud)

然后我收到警告:

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]
Run Code Online (Sandbox Code Playgroud)

如果我换到int没有警告.但我想要一个字节的位域(unsigned char).

怎么样?

oua*_*uah 11

gcc -pedantic如果您不想收到警告,请删除该选项.

在C99中,gcc发出警告,-pedantic但允许为位字段(如unsigned char)提供实现定义类型.

(C99,6.7.2.1p4)"A位字段应具有一种类型,是_Bool的合格或不合格的版本,符号int,unsigned int类型,或其他一些实现定义的类型."

在C90,只有int,unsigned intsigned int允许.

(C90,6.5.2.1)"位字段的类型应为int,unsigned int或signed int之一的限定或非限定版本"

实际上在C90和C99中,C都不需要警告(它仅在C90中是未定义的行为,但C不需要对未定义的行为发出警告).该警告是通过添加gcc-pedantic,仅供参考.