Search code examples
cgetchar

Extremly simple C program,why I am getting output like this?


Code:

#include <stdio.h>
int main()
{
  long cn=1;
  char ch;
  while((ch=getchar())!=EOF)
  {
    printf("%ld\t%c\n",cn++,ch);
  } 
}

When I input word "secret" and hit enter it shows count up to 7 and not 6,why?


Solution

  • Because the "enter" character is read as well. This is in fact a "newline", ASCII code 10 (or hex 0A).