为班级编写程序,仅限于 scanf 方法。程序接收可以接收任意数量的行作为输入。使用 scanf 接收多行输入时出现问题。
#include <stdio.h>
int main(){
char s[100];
while(scanf("%[^\n]",s)==1){
printf("%s",s);
}
return 0;
}
输入示例:
Here is a line.
Here is another line.
这是当前的输出:
Here is a line.
我希望我的输出与输入相同。使用scanf。
我认为你想要的是这样的(如果你真的仅限于 scanf):
#include <stdio.h>
int main(){
char s[100];
while(scanf("%[^\n]%*c",s)==1){
printf("%s\n",s);
}
return 0;
}
%*c 基本上会抑制输入的最后一个字符。
From man scanf
An optional '*' assignment-suppression character:
scanf() reads input as directed by the conversion specification,
but discards the input. No corresponding pointer argument is
required, and this specification is not included in the count of
successful assignments returned by scanf().
[编辑:根据克里斯·多德的抨击删除了误导性答案:)]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)