我正在尝试访问位字段:
下面是运行良好并给出预期结果的代码,但会引发我在下面提到的编译器警告。
#include <stdio.h>
#include <stdint.h>
struct status_type_one{
unsigned delta_cts : 1;// lsb
unsigned delta_dsr : 1;
unsigned tr_edge : 1 ;
unsigned delta_rec : 1;
unsigned cts : 1;
unsigned dsr : 1;
unsigned ring : 1;
unsigned rec_line : 1;// msb
} status_one;
struct status_type_two {
unsigned : 4; // lsb 4 bits
unsigned cts : 1; //bit 5
unsigned dsr : 1; // bit 6
} status_two;
int main(void)
{
status_one.delta_cts=1;
status_one.delta_dsr=0;
status_one.tr_edge=1;
status_one.delta_rec=0;
status_one.cts=1;
status_one.dsr=0;
status_one.ring=1;
status_one.rec_line=1;
printf("The value of status_one is %x\n",status_one); // warning here
status_two.cts=1;
status_two.dsr=1;
printf("The value of status_one is %d\n",status_two); // warning here
return 0;
}
但我收到以下警告:
$ gcc -Wall Bit_Fields.c -o Bit_Fields
Bit_Fields.c: In function `main':
Bit_Fields.c:35: warning: unsigned int format, status_type_one arg (arg 2)
Bit_Fields.c:35: warning: unsigned int format, status_type_one arg (arg 2)
Bit_Fields.c:40: warning: int format, status_type_two arg (arg 2)
Bit_Fields.c:40: warning: int format, status_type_two arg (arg 2)
输出正确,如下图所示
$ ./Bit_Fields
The value of status_one is d5
The value of status_one is 48
任何人都可以告诉,警告是关于什么以及如何解决它?
谢谢