title: Program doesn't wait for user input with scanf("%c",&yn);
body: <p>This is the basic code to a program I am writing to practise using files in C. I am trying to detect whether the output file already exists and if it does exist I want to ask the user if they would like to overwrite it or not. This is the reason that I have first opened the outfilename file in with fopen(outfilename,"r"); as opposed to fopen(outfilename,"w");.</p>
  
  <p>It detects the case of the file not existing, however, if it does exist it executes the printf("Output file already exists, overwrite (y/n):"); statement but completely ignores the scanf("%c",&amp;yn); statement! </p>
  
  <p>The printf at the end of the program reads "yn=0" if the file doesn't exist and just "yn=" if it does exist. Can anybody help me?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;float.h&gt;
  #include &lt;string.h&gt;
  
  int main(void) {
      FILE *inf;
      FILE *outf;
      char filename[21],outfilename[21];
      char yn='0';
  
      printf("Please enter an input filename: ");
      scanf("%s",&amp;filename);
  
      printf("Please enter an output filename: ");    
      scanf("%s",&amp;outfilename);
  
      /* Open file for reading */
      inf=fopen (filename,"r");
      outf=fopen(outfilename,"r");
  
      /*check that input file exists*/
      if (inf!=NULL) {
  
          /*check that the output file doesn't already exist*/
          if (outf==NULL){
              fclose(outf);
              /*if it doesn't already exist create file by opening in "write" mode*/
              outf=fopen(outfilename,"w");
          } else {
              /*If the file does exist, give the option to overwrite or not*/
              printf("Output file already exists, overwrite (y/n):");
              scanf("%c",&amp;yn);
          }
      }
      printf("\n yn=%c \n",yn);
      return 0;
  }
  </code></pre>