以实例问题1.5.2 K&放大器; R本书基于C [英] Problem with example 1.5.2 in K&R book on C

查看:138
本文介绍了以实例问题1.5.2 K&放大器; R本书基于C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我教我下与K&安培; R和我通过书中的例子之一难倒。我正好编译code,因为它是用的例子,但它不会做什么作者说它将。该计划应该算个字符。考虑到code是如下:

I'm teaching myself C with K&R and am stumped by one of the examples in the book. I compile the code exactly as it is written in the example but it does not do what the authors say it will. The program is supposed to count characters. The code given is as follows:

#include <stdio.h>

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

有关它来编译我更换的mai​​n()使用int main()的。但是,我认为是不相关的问题。该程序编译并运行良好。但它只是因为它被写做不计数的字符。我缺少的东西吗?可以在东西已经改变如何现代编译治疗code例如像这样因为本书是写的?任何帮助这个留言板的好乡亲也许能提供将大大AP preciated。

For it to compile I replace main() with int main(). But I assume that is not relevant to the question. The program compiles and runs fine. But it simply does not count characters as it was written to do. Am I missing something? Could something have changed in how modern compilers treat a code example such as this since the book was written? Any assistance the good folks on this message board might be able to offer would be greatly appreciated.

最佳,

推荐答案

之后读文件结束该计划只输出字符数。随着交互式输入,可以生成一个通过CTRL + D文件结束(至少在* NIX,没有关于Windows的想法)。知道了这一点,程序正确这里工作的。

The program only outputs the number of character after it read an "end of file". With interactive input, you can generate an "end of file" via ctrl+d (at least on *NIX, no idea about windows). Knowing this, the program works correctly here.

这篇关于以实例问题1.5.2 K&放大器; R本书基于C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆