0

I am new to programming, and getchar() or EOF in specific. I was testing an example in K&R which used EOF as the termination condition.

#include <stdio.h>

#define EOF 'A'

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

    nwhite = nother =0;

    for(i = 0; i < 10; i++){
        ndigit[i] = 0;
    }

    while((c = getchar()) != EOF){
        switch(c){
            case '0' : case '1' : case '2' : case '3' : case '4' : case '5' :
            case '6' : case '7' : case '8' : case '9' :
                ndigit[c - '0']++;
                break;
            case ' ' : case '\n' : case '\t' :
                nwhite++;
                break;
            default : 
                nother++;
                break;
        }
    }

    printf("digits =");

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

    printf(", white spaces = %d, other = %d\n", nwhite, nother);
    return 0;

}

But I am unable to input EOF in vscode Windows. I have already tried [Ctrl+D], [Ctrl+Z] and other suggested answers elsewhere.

When I define EOF to something like 'A', it puts a warning of redefinition but the program works exactly as intended.

The warning says about the default stdio.h value #define EOF (-1), and I have inputted (-1) also but still doesn't work

mr.loop
  • 818
  • 6
  • 20
  • say `1234 abcd` and in redefined EOF `1234 abcdA`. Basically, it is aprogram that counts occurences of digits, spaces and others – mr.loop Jan 05 '21 at 17:22
  • 1
    you can redirect the input from a file to get an EOF `prog.exe < file` in a terminal or using your IDE to do that (of course remove `#define EOF 'A'`) – bruno Jan 05 '21 at 17:23
  • 1
    I mean did you try abcd1234 ctrl+Z ctrl+Z (2 times), or if you press _ENTER_ after abcd1234 ctrl+Z – IrAM Jan 05 '21 at 17:24
  • 1
    Per the [answers here](https://stackoverflow.com/questions/16136400/how-to-send-eof-via-windows-terminal), try Enter, Ctrl-Z or Ctrl-Z, Enter. You should upvote whichever answer(s) help you there; or, if none of them work, I'd encourage you to write a new one with what does work. – John Kugelman Jan 05 '21 at 17:25
  • 2
    If for some unknown reason, you want to redefine `EOF`, first `#undef EOF` then `#define EOF 'A'` – alex01011 Jan 05 '21 at 17:25
  • 1
    Thanks a lot. `Enter, then Ctrl-Z 2 times, and another Enter` worked – mr.loop Jan 05 '21 at 17:26
  • why are you not using *isdigit* and *isspace* functions ? – bruno Jan 05 '21 at 17:26
  • @bruno I was trying to test something in the code shown in book, but when I copied the code intact, it didn't work in the first place. – mr.loop Jan 05 '21 at 17:27
  • @mr.loop ok I understand – bruno Jan 05 '21 at 17:28
  • 1
    Also, Ctrl-Z 1 time also works accompanied by Enter before and after – mr.loop Jan 05 '21 at 17:30
  • 3
    Just to be pedantic, `EOF` isn't a character as such - it's a *condition* signaled by the various input functions when there's no more data on the input stream, or when an error has occurred during the I/O operation. Note that entering `-1` didn't work because `getchar` reads that as two separate characters - `'-'` followed by `'1'`. It's just more text on the input stream. Also just because `EOF` maps to `-1` in your C code doesn't mean the terminal driver sends a `-1` to your program. – John Bode Jan 05 '21 at 17:33

0 Answers0