简体   繁体   中英

C program doesn't read an input, doesn't give an output

I'm completely new to C and have this example from The C Programming Language book:

#include <stdio.h>

#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */

/* count line and 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);
}

The code compiles fine in the terminal with: gcc -o count count.c When I run it with: ./count it let's me test it, gives me a new line to type in the input. I type in, hit the Return and it gives me another blank line for my text input, not outputting anything. What is it that I do wrong? I was doing some other examples with input and I was receiving an output, but none of the code that I use from this book works for me now. Many thanks.

The value "EOF" is not equal to a newline aka '\\n'. "EOF" is the value sent when you hit ctrl+d, when standard input (aka getchar()) is coming from the keyboard, or when you reach a file, if you're redirecting standard input from a file (which you would do with ./count < file).

假设您正在使用Ctrl-D,这是Linux的EOF字符。

The loop in the code says to iterate till EOF (End-of-File) character is read. If you are using Linux, this would be Ctrl+D. For Windows, you have to press Ctrl+Z and enter key.

If the intended outcome of the program is to count the number of lines, words and characters in the input, then it is important that there should be a clear demarcation of where the given input starts and where it ends.

In this particular program, The input starts when the while loop starts (and the input character is not EOF) and the input stops when the EOF character is entered.

The meaning and usage of EOF has to be clear.

EOF is a macro that is returned by stream functions to indicate an end-of-file condition. This symbol is declared in stdio.h.

When a comparison of the form:

((c = getchar()) != EOF)

is made, what happens internally is that the ASCII value of the character input in 'c' is compared to the ASCII value of EOF.

After you enter the input you hit 'Return'. 'Return' corresponds to New Line of Line feed and the ASCII value for this is 10 (Decimal). EOF in ASCII corresponds to End of Transmission and has the ASCII value 4 (Decimal).

So if you keep hitting 'Return' after the inputs, the program will expect inputs infinitley. To indicate the end of input, EOF has to be input. EOF corresponds to Ctrl-D (ASCII value 4).

So to sum up, there is nothing wrong with your program. All you have to do is at the end of the input, instead of hitting 'Return' , Do a control-D.

Hm,I love K&R . Got this question before, needs a ending. As Ctrl+d, you can try: ls | ./count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM