sduoooh @ 2021-11-05 11:51:23
我这题是这样做的:
#include <stdio.h>
int main()
{
int a,b,c,d;
scanf("%1d%1d%1d.%1d\n",&a,&b,&c,&d);
printf("%1d.%1d%1d%1d",d,c,b,a);
return 0;
}
但是把int改成char就报错:
#include <stdio.h>
int main()
{
char a,b,c,d;
scanf("%1d%1d%1d.%1d\n",&a,&b,&c,&d);
printf("%1d.%1d%1d%1d",d,c,b,a);
return 0;
}
这是为啥啊。。。
by sweetientfond @ 2021-11-05 11:57:07
char 的读入需要使用 %c 而不是 %d
by sduoooh @ 2021-11-05 12:02:33
@Mike_Dreamer 但是用%c的话,不会输出按ASCII码对应的那个字符吗?
by sweetientfond @ 2021-11-05 12:05:11
@sduoooh 所以说输出的时候也应该对应地使用 %c
by sduoooh @ 2021-11-05 12:19:48
@Mike_Dreamer 好的 谢谢 我大概明白了 就是char作为一个获取字符的函数 读入必须是字符对吧 再次谢谢
by ud2_ @ 2021-11-05 12:21:11
报错说得很清楚了
main.c: In function 'main':
main.c:5:14: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'char *' [-Wformat=]
5 | scanf("%1d%1d%1d.%1d\n",&a,&b,&c,&d);
| ~~^ ~~
| | |
| int * char *
| %1hhd
main.c:5:17: warning: format '%d' expects argument of type 'int *', but argument 3 has type 'char *' [-Wformat=]
5 | scanf("%1d%1d%1d.%1d\n",&a,&b,&c,&d);
| ~~^ ~~
| | |
| int * char *
| %1hhd
main.c:5:20: warning: format '%d' expects argument of type 'int *', but argument 4 has type 'char *' [-Wformat=]
5 | scanf("%1d%1d%1d.%1d\n",&a,&b,&c,&d);
| ~~^ ~~
| | |
| int * char *
| %1hhd
main.c:5:24: warning: format '%d' expects argument of type 'int *', but argument 5 has type 'char *' [-Wformat=]
5 | scanf("%1d%1d%1d.%1d\n",&a,&b,&c,&d);
| ~~^ ~~
| | |
| int * char *
| %1hhd
int
和 char
,甚至 signed char
都可以用,但一定要与格式串匹配。
by Zhetengtiao @ 2021-11-05 16:38:27
格式不对啊
改用cin或修改格式
by sduoooh @ 2021-11-09 23:11:28
@ud2_ 好的!谢谢!
by sduoooh @ 2021-11-09 23:12:28
@Zhetengtiao 谢谢,但是我是用c的。。。
by a15083550145 @ 2021-11-27 14:54:10
请问用int的话为什么是%ld而不是%d呢?
by sduoooh @ 2022-02-17 23:27:29
@a15083550145 我用的是1d
不是ld
呀。(手动笑哭)