title: getchar() function in c
body: <p><strong>I have just started learning I/O file operations in c</strong> from <strong>programming in c</strong> by <strong>stephen Kochan</strong>. In one of the exercise questions like the one below </p>
  
  <blockquote>
    <p>Write a program that displays the contents of a file at the terminal 20 lines at a time. At the end of each 20 lines, have the program wait for a character to be entered from the terminal. If the character is the letter q, the program should stop the display of the file; any other character should cause the next 20 lines from the file to be displayed.</p>
  </blockquote>
  
  <pre><code>#include&lt;stdio.h&gt;
  int main(void)
  {
    int count=0,c;
    FILE *fname;
    char name[64];
    char again='a';
    printf("enter the name of file to be read : ");
    scanf("%s",name);
    if((fname=fopen(name,"r"))==NULL){
      printf("file %s cannot be opened for reading \n",name);
      return 1;
    }
    while(again!='q'){
        count=0;
        while((c=getc(fname))!=EOF)
            {
              if(c!='\n')
                {
                  putchar(c);
                }
              else{
                putchar('\n');
                count++;
                printf("count = %i\n",count); //debug statement
                }
              if(count&gt;19)
                break;
            }
        again=getchar();
        printf("again = %c\n",again); //debug statement
      }
  fclose(fname);
  printf("\n");
  return 0;
  }
  </code></pre>
  
  <p>In the above program, when i looked at the output initially, the program was displaying 40 numbers without taking a break at 20 numbers, so i have included some debugging statements in the above to see where i am going wrong and the output i was getting was:</p>
  
  <pre><code>count = 1
  2
  count = 2
  3
  count = 3
  4
  count = 4
  5
  count = 5
  6
  count = 6
  7
  count = 7
  8
  count = 8
  9
  count = 9
  10
  count = 10
  11
  count = 11
  12
  count = 12
  13
  count = 13
  14
  count = 14
  15
  count = 15
  16
  count = 16
  17
  count = 17
  18
  count = 18
  19
  count = 19
  20
  count = 20
  again = //it skipped the loop the first time
  
  21
  count = 1
  22
  count = 2
  23
  count = 3
  24
  count = 4
  25
  count = 5
  26
  count = 6
  27
  count = 7
  28
  count = 8
  29
  count = 9
  30
  count = 10
  31
  count = 11
  32
  count = 12
  33
  count = 13
  34
  count = 14
  35
  count = 15
  36
  count = 16
  37
  count = 17
  38
  count = 18
  39
  count = 19
  40
  count = 20
  q
  again = **need to input here**
  </code></pre>
  
  <p>So for the first time, the <code>getchar()</code> is not prompting for an input. So i replaced the part where <code>getchar()</code> is with:</p>
  
  <pre><code>scanf(" %c",&amp;again);
  </code></pre>
  
  <p>It's working fine as expected. The program is prompting for an input <strong>for the first time after the 20 new lines</strong>. I also left some whitespace so that <code>scanf</code> would ignore it. So long post short, I don't think i had understood the behavior of <code>getchar()</code> completely. I am trying to learn these things myself, i Googled for an explanation but i came up empty. Any help and feedback about this would be really appreciated.</p>