Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

arrays - Receiving no errors but not seeing an output

I'm currently learning C from an analog copy of The C Programming language. While it has been helpful in understanding the fundamental aspects of programming, I'm having trouble in understanding how my programs copied directly from the examples in the textbook are not producing an output in the terminal, nor are they explicitly requesting an input. I figured there's a giant hole in my comprehension of what's going on.

Currently I'm on section 1.6 on arrays. Here's the example code:

#include <stdio.h>

int main()

{
    int c, i, nwhite, nother;
    int ndigit[10];

    nwhite = nother = 0;
    for (i = 0; i < 10; ++i)
        ndigit[i] = 0;

    while ((c = getchar()) != EOF)
        if (c >= '0' && c <= '9')
            ++ndigit[c-'0'];
        else if (c == ' ' || c == '
' || c == '')
            ++nwhite;
        else 
            ++nother;

    printf("digits =");
    for (i = 0; i < 10; ++i)
        printf(" %d", ndigit[i]);
    printf(", white space = %d, other = %d
", nwhite, nother);

 }

In the text since it's C89, it didn't include the int before main(), which I added. It's not spitting out any errors, but it also isn't spitting out anything else after I compile it. What am I missing?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I just tested the code and it does work.

I want to make sure that you are running the compiled file. After you compile a code file that ends in .c, there will be an executable file that you have to run to make the compiled program actually execute.

If you are running the compiled file, then I think the problem you are having is that the program keeps trying to get chars until it reads EOF (end of file). The line "while ((c = getchar()) != EOF)" accomplishes 2 things. First, it gets a character from standard input, storing it in the variable "c". Second, it breaks out of the loop only when the variable "c" contains EOF. The print statements below the while loop will never be reached if the program never reads EOF from standard input. If your input is coming from the terminal (this is the default setting fore standard input), you will need to make the program read EOF manually by pressing Ctrl-D (if you are using Linux or Unix) or Ctrl-Z (if you are using Windows).

I noticed that you said that the program is not "explicitly asking for input." This program doesn't have any print statements before the calls to "getchar()". When this program runs, it won't give much of an indicator that it is expecting input because there is no code that prints text to the terminal asking for input. Once you run the code, try just typing the input and pressing the "enter" key after each input.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...