Scanf导致C程序崩溃

这个简单的问题导致我的整个程序在第一次输入时崩溃。 如果我删除输入,程序工作正常,但一旦我将scanf添加到代码并输入输入程序崩溃。

#include  #include  #include  #define MAXEMPS 3 // stub program code int main (void){ char answer; do { printf("\n Do you have another(Y/N): "); scanf("%c", answer); }while(answer == 'Y' || answer == 'y'); getchar(); printf(" Press any key ... "); return 0; } // main 

您必须将变量的地址传递给scanf:

  scanf("%c", &answer); 

使用“和答案”。 并摆脱无关的“fflush()”命令……

更好,替换“answer = getchar()”。