本文介绍了我为什么需要preSS CTRL + D两次打出来的`而((C =的getchar())!= EOF)`在Ubuntu 14.10?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C编程和Ubuntu。我被D.M里奇看了C程序设计语言,其中我发现下面的code:

I am new to C Programming and Ubuntu. I was reading the "The C Programming Language" by D.M Ritchie where I found the following code:

#include <stdio.h>

int main()
{
    int c;
    int nc=0;
    while((c = getchar()) != EOF)
    {
        nc++;
    }

    printf("%d Characters \n",nc);

    return 0;
}

但在运行程序时我输入你好,然后 CTRL + D 两次的拿到这5个字符的实际数量。

But while running the program I enter "Hello" ,then twice to get the actual number of characters which is 5.

但是,当我输入你好,然后 CTRL + D 一次的,什么也没有发生,终端仍等待输入。

But when I enter "Hello" then once, nothing happens, the terminal still waits for input.

为什么?

推荐答案

引用<一个href=\"http://stackoverflow.com/questions/29746522/why-is-some-$c$c-from-kr-not-working-in-$c$cblocks/29746577#comment47630286_29746577\">@Veritas's评论,

在Linux上按Ctrl-D只能当缓冲区已为空,否则它只是刷新它。因此,除非他有$ P $之后没有任何字符pssed进入,他将不得不preSS按Ctrl-D的两倍。

这说明了问题。你必须preSS两次,因为你,键入您好之后,并没有preSS的输入到输入水冲到标准输入。所以你第一次preSS CTRL + D ,它刷新数据到标准输入。您preSS第二次呢, EOF 发送。

This explains the issue. You have to press it twice because you , after typing Hello, did not press the to flush the input into the stdin. So the first time you press , it flushes the data into the stdin. The second time you press it, EOF is sent.

这篇关于我为什么需要preSS CTRL + D两次打出来的`而((C =的getchar())!= EOF)`在Ubuntu 14.10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:51
查看更多