新手C题 [英] Newbie C Problem

查看:117
本文介绍了新手C题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我第C经历一本书,在书中,我不能去工作所遇到的一个例子。

Right now I am going through a book on C and have come across an example in the book which I cannot get to work.

#include <stdio.h>
#define IN 1
#define OUT 0
main()
{
  int c, nl, nw, nc, state;
  state = OUT;
  nl = nw = nc = 0;
  while ((c = getchar()) != EOF) {
    ++nc;
    if (c == '\n')
      ++nl;
    if (c == ' ' || c == '\n' || c == '\t')
      state = OUT;
    else if (state == OUT) {
      state = IN;
      ++nw;
    }
  }
  printf("%d %d %d\n", nl, nw, nc);
}

它应该算输入中的线条,文字,字符数。然而,当我在终端中运行它,它显得无可奈何。我缺少的东西还是有这个code的一个问题?

Its supposed to count the number of lines, words, and characters within an input. However, when I run it in the terminal it appears to do nothing. Am I missing something or is there a problem with this code?

推荐答案

程序只终止当输入端(返回的getchar EOF)。当在终端上运行,这通常不会发生正因为如此看来,该方案被卡住。你需要通过pressing按Ctrl + D(可能是两次)在Linux或pressing F6手动关闭输入,并在Windows上的行的开头输入(不同的系统可能使用不同的手段这一点)。

The program only terminates when the input ends (getchar returns EOF). When running on terminal, this normally never happens and because of this it seems that the program is stuck. You need to close the input manually by pressing Ctrl+D (possibly twice) on Linux or pressing F6 and Enter at the beginning of the line on Windows (different systems may use different means for this).

这篇关于新手C题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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