[英]Word count program not working. Code sample From the Book The C-programming Language Ritchie & Kernighan
這是字數統計程序的代碼示例。 但它不起作用。 當我們執行它時,輸入單詞后它應該顯示結果,但它沒有產生任何東西。這段代碼中缺少什么嗎?
#include<stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* counts lines, words, and characters in input */
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);
}
你的代碼沒問題。 您必須問自己如何打破 while 循環,因為它不斷讀取輸入,即如何將EOF
發送到您的程序。
在 *nix 系統上,您執行CTRL+D,而在 Windows 上,您執行CTRL+Z以生成 EOF。
另外:使用main()
的標准簽名之一,例如int main(void)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.