为什么这个y/n程序没有输出

isl*_*key 0 c string conditional stdin

我有一个小C程序的问题.它输出一个问题(见下面的代码),我可以将输入放入(y和n),但之后没有其他任何事情发生,即使它是根据输入的输入(y或n)打印某些东西.但是,在我的问题之后没有输出任何内容,程序就会退出.这是代码:

#include <stdio.h>

int main()
{
        char string [80];
        static char y;
        static char n;
        printf( "ARE YOU SHAQIRI? [y/n]: " );
        scanf( "%s", string );
        if ("%s" == "y")
                printf("That's impossible. YOU CANNOT BE SHAQIRI YOU IDIOT");
        else if ("%s" == "n")
              printf("I thought not.");
        fflush ( stdin );
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

pb2*_*b2q 6

您的比较中有两个问题if ("%s" == "y"):

  • "%s"是你的scanf 格式字符串.如果scanf成功读取输入,则结果在您的变量中:string.
  • 不要==用来比较字符串.你应该用strcmp.

因为您在两个if测试中都使用此表单的比较,所以两个分支都不执行,并且您看不到输出.

也不要叫fflushstdin.你可能打算fflush(stdout)在那里.