getchar()
。在下面的程序中,getchar
按预期工作:#include <stdio.h>
int main()
{
printf("Type Enter to continue...");
getchar();
return 0;
}
然而,在下面的程序中,getchar
不会创建延迟,程序会立即结束:
#include <stdio.h>
int main()
{
char command[100];
scanf("%s", command );
printf("Type Enter to continue...");
getchar();
return 0;
}
我有以下奇怪的解决方法,它可以运行,但我不明白为什么:
#include <stdio.h>
int main()
{
char command[100];
int i;
scanf("%s", command );
printf("Type Enter to continue...");
while ( getchar() != '\n') {
i=0;
}
getchar();
return 0;
}
所以我的问题是:
1. scanf
是做什么的?为什么 scanf
要这样做?
2. 为什么我的解决方法有效?
3. 模拟以下 Python 代码的好方法是什么:
raw_input("Type Enter to continue")
getchar()
将立即返回。 - Pablo