[当我运行该命令时,一切看起来都很好,在for循环中两次打印“写入字符”,然后等待输入。据我了解,这些值并没有真正分配(即使在第一次迭代中也没有)。有帮助吗?
您的scanf
函数正在捕获宽松的字符,也许是从printf
字符串之前的,使用getchar()
可以解决此问题,如下所示:
#include <stdio.h>
int main(){
int i; char a[100];
for(i=1; i<=5; i++){
printf("print char\n");
scanf("%c", &a[i]);
getchar();
}
}
fflush()
应该有效,但是在您的代码中无效。