字符计数程序不输出任何东西? [英] Character counting program not outputting anything?

查看:32
本文介绍了字符计数程序不输出任何东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉问这么简单的问题,我还在学习 C 并先了解基础知识.

Sorry for asking such a simple question, I'm still learning C and going through the basics first.

我正在创建一个字符计数程序,但是当我执行该程序并尝试输入h"然后按回车键时会出现一个新行,但该行没有输出任何内容?

I'm creating a character counting program and yet when I execute the program and try to input "h" for example and then press enter a new line appears and nothing is outputted onto that line?

代码:

#include <stdio.h>

/* Copy input and count characters 2nd version */

main() {
    double cc;
    for(cc = 0; getchar() != EOF; ++cc);
    printf("%.0f\n", cc);
}

推荐答案

完成字符输入后,必须按 Ctrl-D 表示输入流结束.否则您的程序将继续等待更多输入.

Once you have finished entering characters, you have to signal the end of input stream by pressing Ctrl-D. Otherwise your program will continue waiting for more input.

附言为什么要为计数器使用 double 变量?整数类型会更合适.

P.S. Why are you using a double variable for the counter? An integer type would be more appropriate.

这篇关于字符计数程序不输出任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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