想要在不将数据类型更改为 char 的情况下删除警告
Want to remove warning without change data-type to char
我想在不将数据类型更改为 char 的情况下删除警告。
#include<stdio.h>
#include<stdlib.h>
main()
{
unsigned char ch;
printf("Hello This is Problematic\n");
scanf("%d",&ch);
printf("1\n");
}
这会生成警告
test.c:7:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘unsigned char *’ [-Wformat=] scanf("%d",&ch);
实际上,
scanf("%d",&ch);
使您的程序保留 undefined behavior,因为提供的参数不是转换说明符的正确类型。你需要写
scanf("%hhu",&ch);
引用 C11
,章节 §7.21.6.2
hh
Specifies that a following d
, i
, o
, u
, x
, X
, or n
conversion specifier applies
to an argument with type pointer to signed char
or unsigned char
.
我想在不将数据类型更改为 char 的情况下删除警告。
#include<stdio.h>
#include<stdlib.h>
main()
{
unsigned char ch;
printf("Hello This is Problematic\n");
scanf("%d",&ch);
printf("1\n");
}
这会生成警告
test.c:7:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘unsigned char *’ [-Wformat=] scanf("%d",&ch);
实际上,
scanf("%d",&ch);
使您的程序保留 undefined behavior,因为提供的参数不是转换说明符的正确类型。你需要写
scanf("%hhu",&ch);
引用 C11
,章节 §7.21.6.2
hh
Specifies that a following
d
,i
,o
,u
,x
,X
, orn
conversion specifier applies to an argument with type pointer tosigned char
orunsigned char
.