我想输入两个字符串,在输出这两个字符串。 这样写代码
#include<stdio.h>
int main(){
int i=2;
char s[100]={0};
while(i--)
{
scanf("%s",&s);
printf("%s\n",s);
}
return 0;
}
假如输入:abce
abcd
结果是:abce
abce
abcd
abcd
但是当我用scanf("%[^\n]",&s)输入带空格的字符串时,比如代码是这样:
#include<stdio.h>
int main(){
int i=2;
char s[100]={0};
while(i--)
{
scanf("%[^\n]",&s);
printf("%s\n",s);
}
return 0;
}
当我输入:abce abcd,在按下回车后,还没来得及输入第二个想输入的字符串,就立刻连续输出了两次abce abcd
显示的结果就像这样:
abce abcd
abce abcd
abce abcd
这是为什么呢?