【发布时间】:2012-09-29 15:11:31
【问题描述】:
我很难理解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")
【问题讨论】: