为什么不的getchar()返回认作为EOF在控制台上? [英] Why doesn't getchar() recognise return as EOF on the console?

查看:124
本文介绍了为什么不的getchar()返回认作为EOF在控制台上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code下面的一个小片段,我使用的是运行 PellesC

I have a small snippet of code below that I'm running using PellesC.

在执行code和我输入了几个字符到控制台,我preSS进入。

When the code is executed and I've typed a few characters into the console, I press enter.

你能向我解释为什么的printf(%LD \\ N,NC); 行似乎并没有得到执行?由于没有输出被写入控制台。

Can you explain to me why the printf("%ld\n", nc); line doesn't seem to get executed? As no output is written to the console.

#include <stdio.h>

int main(void)
{
    long nc = 0;

    while(getchar() != EOF)
    {
        ++nc;
    }

    printf("%ld\n", nc);
}

我已经决定要学完使用K&放C;:R的书,我很不好意思地说这个,而基本的例子已经难倒我。

I've decided to learn C thoroughly using the K&R book and I'm embarrassed to say this rather elementary example has me stumped.

推荐答案

pressing进入实际上并没有导致EOF(文件结束)。你必须表明你与提供输入彻底完蛋了;在Unix上通常那样由pressing Ctrl + D键。在Windows上我相信这是按Ctrl + Z依次输入,但我真的不知道这一点。

Pressing enter doesn't actually cause an EOF ("end of file"). You have to signal that you are completely finished with providing input; on Unix you typically do that by pressing Ctrl+D. On Windows I believe it's Ctrl+Z followed by enter, but I'm really not sure about that.

这篇关于为什么不的getchar()返回认作为EOF在控制台上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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