我想在不将数据类型更改为 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
.
关于c - 想要在不将数据类型更改为 char 的情况下删除警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42802743/