【发布时间】:2016-03-02 14:34:42
【问题描述】:
我有以下代码。我在想我应该一次只输入一个字符。但即使我在一行中输入一个像 hello 这样的字符串,它也能正确接受。为什么是这样?是否与标准输入缓冲区刷新问题有关?
#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 这样的字符串,它会正确接受" --> 这与您的预期有何不同?