【问题标题】:Extremly simple C program,why I am getting output like this?非常简单的C程序,为什么我得到这样的输出?
【发布时间】:2015-10-12 13:00:25
【问题描述】:

代码:

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

当我输入单词“secret”并按回车时,它显示计数为 7 而不是 6,为什么?

【问题讨论】:

标签: c getchar


【解决方案1】:

因为“输入”字符也会被读取。这实际上是一个“换行符”,ASCII 码 10(或十六进制 0A)。

【讨论】:

  • 谢谢,我正在阅读 Ritchie C 的书并为 www.embeddedfunda.com 做一个测验
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
相关资源
最近更新 更多