我尝试运行此代码:
#include <stdio.h>
int main() {
char str1[20], str2[30];
printf("Enter name: ");
scanf("%s", str1);
getc(stdin);
printf("Enter your website name: ");
scanf("%s", str2);
getc(stdin);
printf("Entered Name: %s\n", str1);
printf("Entered Website:%s", str2);
return(0);
}
来自http://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm
所以我希望在控制台中得到这个:
Enter name: admin
Enter your website name: admin.com
Entered Name: admin
Entered Website: admin.com
但实际上我在我的控制台中得到了这个:
admin
admin.com
Enter name: Enter your website name: Entered Name: admin
Entered Website:admin.com
所以我想知道为什么scanf在打印之前执行了。 也许它与使用eclipse作为IDE有关?
答案 0 :(得分:1)
尝试在printf和scanf之间添加fflush(stdout);
。
答案 1 :(得分:0)
这是一个编译器问题 - 我只在 eclipse 中发生过。这是因为它试图复制 unix 编译,因此有一些奇怪的怪癖
每次你想看到输出后,放置 fflush(stdout);