来自 K&R 的字符计数示例

标签 c eof kernighan-and-ritchie

<分区>

K&R

字符计数示例 我无法获得书中预期的输出。例如,当我输入“我回来了”并按回车键时,它应该会给我一个答案,但为什么不会?

#include <stdio.h>
/* count characters in input; 1st version */
main()   {
   long nc;
   nc = 0;
   while (getchar() != EOF)
       ++nc;
   printf("%ld\n", nc);
}

最佳答案

我认为您没有看到输出,因为您没有从键盘发送 EOF。因此,尝试通过按 Ctrl + D 来发送它。

关于来自 K&R 的字符计数示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21663510/

相关文章:

c - 用于解析 uevent 的 posix 正则表达式导致错误

c - Valgrind macOs 和错误系统调用参数 msg->desc.port.name 指向未初始化的字节

bash 命令行中的自定义 readline 函数?

c - 写文件时 "manually"怎么表示EOF?

c - 如何转换此 "for"循环以在 C 中使用 EOF?

c - 提取数组中的唯一元素(来自 K 和 R C ex1-14)

c - 在目录上使用 `read` 系统调用

c - 如何通过主函数通过用户输入来填充在函数中定义为参数的数组?

Android:为什么 BufferedReader 读取 EOF?

c - 关于 Kernighan 和 Ritchie 练习 2-3 的问题