我很难理解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")