1
//first example
void readInWBW()
{
    int ch; 
    while((ch=getchar()) != EOF)
        putchar(ch);
}

when I input "qweCTRL+D", the first time Where I input ctrl+z just flush the buffer, and I re-input "ctrl+d" so it's like "qweCTRL+DCTRL+D", then the EOF work, program terminate. result is

$ ./a.out
qweqwe$

//second example
void guess()
{
    int guess = 1;  
    printf("Pick an integer from 1 to 100. I will try to guess ");
    printf("it.\nRespond with a y if my guess is right and with");
    printf("\nan n if it is wrong.\n");
    printf("Uh...is your number %d?\n", guess);
    while (getchar() != 'y'){      //<---------------------------
        printf("Well, then, is it %d?\n", ++guess); //<----------
        sleep(1);
    }
    printf("I knew I could do it!\n");
}

In this example, I input "qweCTRL+D", it will shows three times "Well, then...", but if I input CTRL+D again, the program will get into infinite loop. reuslt is:

Pick an integer from 1 to 100. I will try to guess it.
Respond with a y if my guess is right and with
an n if it is wrong.
Uh...is your number 1?
qweWell, then, is it 2? //<--------------------------
Well, then, is it 3?
Well, then, is it 4?
Well, then, is it 5?
Well, then, is it 6?
Well, then, is it 7?
Well, then, is it 8?
Well, then, is it 9?
Well, then, is it 10?
Well, then, is it 11?
^C

//third example    
void test()
{
    char ch;
    while((ch = getchar()) != '#')
        putchar(ch); 
}

I tried to input "qweCTRL+D" like other example, but after flush buffer, "CTRL+D" not response anymore, even if I input "#", it still not terminate. result is:

$ ./a.out
qweqwe
#
#
^C
$

I don't understand why in example2 and example3 have infinite loop and can't terminate the program. can anyone explains it, thank you.

Beywal
  • 13
  • 3

1 Answers1

0

Let's focus on your third example.

void test()
{
    char ch;
    while((ch = getchar()) != '#')
        putchar(ch); 
}

Remember that inputting C-d means "end of file", so it's very natural that the program stops responding to input. You have said that there is no more input. That's what EOF means.

So when you have inputted C-d, what will happen is that ch = getchar() will constantly read EOF. Whatever you type after C-d will not go into the input stream. So the simple answer to why you enter an infinite loop is the simple fact that EOF != '#' in combination with the fact that getchar() always will read EOF after it has read it once.

Another thing that might be good to mention here is that you are using buffered input. So getchar() will simply wait until there is something in stdin. When you enter a few characters, they will not be flushed to stdin until you press enter OR C-d.

Also, remember that EOF does not fit in a char. The function getchar returns an int which is either a number that fits in a char or EOF. The variable ch should be declared as an int and properly checked for EOF.

It is possible to reset stdin, although I'm not sure if it's possible in a portable way. But here is a question about that: How to restart stdin after Ctrl+D?

klutt
  • 30,332
  • 17
  • 55
  • 95
  • thank you, it's very helpful, I tried to insert printf("1") after " while((ch = getchar()) != '#') putchar(ch); " in third example, and it get into infinite loop too as your explained. – Beywal Sep 05 '20 at 09:12