我有以下代码。我以为我一次只能输入一个字符。但即使我在一行中输入喜欢的字符串作为输入,它也能正确接受。为什么是这样?它与标准输入缓冲区冲刷问题有关吗?当指定的格式是字符时接受字符串
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
int main()
{
char sourceString [100];
int index=0;
printf("Enter the characters one by one enter * to stop\n");
do
{
scanf("%c",&sourceString[index]);
index++;
} while (sourceString[index-1]!='*');
index=0;
while (sourceString[index]!='*')
{
printf("%c",sourceString[index]);
index++;
}
printf("\n");
return(0);
}
“如果我给一个字符串像hello作为一行输入,它会正确接受” - >这与你所期望的不同吗? – chux