islandmonkey islandmonkey - 7 months ago 59
C Question

Why is there no output from this y/n program

I have a problem with a small C program. It outputs a question (see code below) of which I can put input into (y and n) but then nothing else happens, even though it is meant to print something according to the input entered (y or n). However, nothing is outputted after my question, and the program just exits. Here is the code:

#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;


You have two problems in your comparison: if ("%s" == "y"):

  • "%s" is your scanf format string. If scanf succeeds in reading the input, then the result is in your variable: string.
  • Don't use == to compare strings. You should use strcmp.

Because you use a comparison of this form in both if tests, neither branch executes, and you see no output.

Also don't call fflush on stdin. You may intend fflush(stdout) there.