2

I have started working through "The C programming language, 2nd Ed." & already I'm running into problems. This code is from Chapter 1 on arrays,

#include <stdio.h>

int main(void){

    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 == '\n' || c == '\t')
            ++nwhite;
        else
            ++nother;
    }

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

It compiles (gcc via Visual Studio Code, Win 10, x64) no errors or warnings. I input the following sequence via the keyboard,

12333 45666 789

and the output I get,

digits = 0^C

Now I was expecting,

digits = 0 1 1 3 1 1 3 1 1 1, white space = 2, other = 0

I then rewrote the for loop that is supposed to print the array to print out i

for (i = 0; i < 10; ++i){
        printf(" %d", i);
}

Then got,

digits = 0^C

What's going on here? First off, why isnt the array being printed, secondly why is the final printf() statement not being executed?

EDIT:: Thanks to the comments, I didn't understand the EOF character properly & how the input stream works. If I exit with ctrl+z and then enter the program executes correctly!

DrBwts
  • 3,470
  • 6
  • 38
  • 62
  • 5
    `^C` indicates the program received a ctrl-c input and aborted the program. – Mark Tolonen Aug 09 '21 at 13:13
  • When I compiled your code using both gcc and cl, all the program seemed to do was recieve input until the program stopped. I don't see how you got your output above. – BlueStaggo Aug 09 '21 at 13:16
  • Your programming is not coming out of while loop, Hence you are using ctrl+c to terminate the program - which prints ^C. – Sudhee Aug 09 '21 at 13:16
  • 3
    Whereas `EOF` is supplied as an input flag value after Ctrl-D (Linux) or Ctrl-Z (Windows). – Weather Vane Aug 09 '21 at 13:17
  • How did you expect your `while` loop to end? – Support Ukraine Aug 09 '21 at 13:17
  • 1
    For fun do: `while ((c = getchar()) != EOF){` --> `while ((c = getchar()) != '9'){` and give the same input... – Support Ukraine Aug 09 '21 at 13:19
  • Your program is probably designed to be called in such a way that it receives its input not from the keyboard, but from a file. Try calling your program like this `"myprogramname < myinputfile.txt"`. That way, your program will automatically receive `EOF` when the file ends, and you don't have to press any key combination to end the loop. – Andreas Wenzel Aug 09 '21 at 13:20
  • https://stackoverflow.com/questions/4358728/end-of-file-eof-in-c – Support Ukraine Aug 09 '21 at 13:21
  • In previous examples in the book when I've used ctrl+c to terminate the `while` loop the `printf()` commands after the loop have executed as expected but not in this case. – DrBwts Aug 09 '21 at 13:30
  • 2
    This program reads from standard input until an EOF is encountered. If you're running it from a terminal, then ^D is the standard way to end the input (at least on Unix systems). If you type ^C instead, you are aborting the program before it can complete. I think someone above said ^Z works for Windows. – Tom Karzes Aug 09 '21 at 13:31
  • @4386427 now it all works! – DrBwts Aug 09 '21 at 13:31
  • OK thanks for the comments, I've edited the OP accordingly. – DrBwts Aug 09 '21 at 13:35
  • 2
    With Ctrl-Z so on Windows, I have found it is quirky, and the sequence has to be Enter, Ctrl-Z, Enter, IOW the Ctrl-Z was preceded by an Enter too. – Weather Vane Aug 09 '21 at 13:38
  • How are you generating the end-of-file character to terminate the read? – TRPh Aug 09 '21 at 14:23

0 Answers0