简体   繁体   English

极其简单的C程序,为什么我会得到这样的输出?

[英]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? 当我输入单词“ secret”并按回车键时,它显示计数最多为7,而不是6,为什么?

Because the "enter" character is read as well. 因为也会读取“输入”字符。 This is in fact a "newline", ASCII code 10 (or hex 0A). 实际上,这是一个“换行符”,ASCII码10(或十六进制0A)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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