K&R 书中的 C 编程练习

发布于 2024-07-10 08:40:14 字数 264 浏览 5 评论 0原文

知道为什么以下代码不打印输入中的字符数吗? 这是我直接从 K&R 书中摘录的。 目前正在学习 C,这真的很令人困惑,在我看来我永远不会达到 EOF。 如果是这样的话,为什么要用这个作为例子呢?

#include <stdio.h>

main()
{
    double nc;

    for (nc = 0; getchar() != EOF; ++nc)
        ;
    printf("%d\n", nc);
}

Any idea why the following code doesn't print the amount of characters in the input?
I've taken this straight from the K&R book. Learning C at the moment and this is really confusing, looks to me like I'm never reaching EOF. If that's the case then why would this be used as an example?

#include <stdio.h>

main()
{
    double nc;

    for (nc = 0; getchar() != EOF; ++nc)
        ;
    printf("%d\n", nc);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

花想c 2024-07-17 08:40:14

程序看起来正确,您的问题是您必须在输入后将 EOF(文件结束符)发送到命令行,因为标准输入被视为文件。 我认为在 Linux 终端中您可以按 Ctrl+D 发送 EOF...我不知道如何在其他操作系统中执行此操作。 要测试此程序,您可能需要将 EOF 更改为 '\n',这将导致它在您按 Enter 时停止。

The program looks correct, your problem is that you have to send EOF (end-of-file) to the command line after your input since the standard input is treated as a file. I think in a linux terminal you can press Ctrl+D to send EOF... I'm not sure how to do it in other OS's. To test this program you might want to change EOF to '\n' which will cause it to stop when you press enter.

记忆里有你的影子 2024-07-17 08:40:14

程序不断从 stdin 读取数据,直到收到 EOF,这是通过在 Unix 控制台上的行开头按 Ctrl-D 或在行首按 Ctrl-Z 来完成的。 Windows 控制台中一行的开头。 以这种方式发出 EOF 信号后,将执行 printf 函数,显示读取的字符数。

The program keeps reading data from stdin until EOF is received, this is done by pressing Ctrl-D at the beginning of a line on a Unix console or Ctrl-Z at the beginning of a line in a Windows console. After you signal EOF in this way, the printf function will be executed displaying the number of characters read.

彼岸花似海 2024-07-17 08:40:14

您的 nc 是一个双精度数 — 使用 printf("%lf", nc) 打印双精度数。

试试这个

#include <stdio.h>

int main(void)
{
    int nc;

    for (nc = 0; getchar() != EOF; ++nc)
        ;
    printf("%d\n", nc);

    return 0;
}

Your nc is a double — use printf("%lf", nc) to print doubles.

Try this one instead

#include <stdio.h>

int main(void)
{
    int nc;

    for (nc = 0; getchar() != EOF; ++nc)
        ;
    printf("%d\n", nc);

    return 0;
}
余厌 2024-07-17 08:40:14

我想澄清到目前为止给出的答案,因为他们似乎使用“发送 EOF”、“收到 EOF”、“EOF 字符”等短语。根据对此答案的评论(感谢),“发送 EOF”和“received EOF”是合法的术语,但请不要认为它是一个字符。

EOF 根本不是一个字符。 如果流位于“文件结尾”或发生读取错误,则 getchar()(或 fgetc/getc)返回该值。 它只是 getchar() 将返回的字符值范围之外的一个特殊值,指示错误或文件结束的条件

C 标准将其定义为负数,而 getchar 将字符作为转换为 int 的 unsigned char 返回。

编辑:在做了一些我应该在我写的段落之前完成的研究时,我意识到我的一些假设是完全错误的。 感谢评论者指出这一点。

一旦流(例如 stdin)处于文件结束状态,可以使用 clearerr() 再次清除此情况,并且 getchar() 可以从 stdin 读取更多数据。

I'd like to clarify the answers given so far because they seem to use phrases like "send EOF", "received EOF", "EOF character", etc. As per comments (thanks) to this answer, "send EOF" and "received EOF" are legitimate terms, but please don't think that it's a character.

EOF is not a character at all. It is the value that getchar() (or fgetc/getc) returns if the stream is at "end-of-file" or a read error occurs. It is merely a special value outside the range of character values that getchar() will return that indicates the condition of error or end-of-file.

It is defined by the C standard as being negative, whereas getchar returns characters as an unsigned char converted to int.

Edit: On doing some research which I should've done before the paragraph I wrote that used to be here, I've realised some of my assumptions were completely wrong. Thanks to the commenter for pointing this out.

Once a stream (such as stdin) is in end-of-file condition, this condition can be cleared again with clearerr() and getchar() may read more data from stdin.

强辩 2024-07-17 08:40:14

该代码从标准输入读取字符。 当你正常运行这个程序时,标准输入来自用户。 在这种情况下,没有办法向程序发送EOF。

如果您将文件重定向到 (./myprogram < tempfile.txt) 中的标准,则此代码将起作用。

This code reads characters from the standard input. When you run this program normally, standard input comes from the user. In this case, there is no way to send EOF to the program.

This code will work if you redirect a file to the standard in (./myprogram < tempfile.txt).

肥爪爪 2024-07-17 08:40:14

首先,如前所述,您需要 %lf 而不是 %d 来显示双精度数,否则它将始终显示零(d 表示有符号整数)。 然后,如果您想在基于 Windows 的操作系统上手动测试此操作,请输入某些字符,然后按 Enter,然后在下一行上单独按 CtrlZ(这将模拟 EOF)。 打印结果将比字符数(包括 CtrlZ)多 1。 另外,正如 SoapBox 所指出的,在查找 EOF 时,当时的想法通常是 Unix,通过标准输入将文件传输到程序中,这也是可行的。

First, as mentioned previously you need %lf instead of %d to display the double, otherwise it will just display zero all the time (d is for a signed integer). Then if you want to test this manually on a windows based OS type in some characters press Enter and then pressl CtrlZ on the next line all by itself (this will simulate the EOF). The printed result will be one more than the number of characters (it includes the CtrlZ). Also as SoapBox indicated when looking for an EOF typically the thinking back then was Unix where you pipe a file into the program via standard input, this would also work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文