简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM