1

While reading input character by character, CTRL-D should activate the EOF symbol and exit the loop and execute the printf statement after the loop. However, CTRL-D exits the program entirely.

#include <stdio.h>
#include <ctype.h>

int main() {

    int current_character, next_character;

    int amount_of_characters = 0, amount_of_words = 0, amount_of_newlines = 0;

    while( (current_character = getchar()) != EOF) {

         amount_of_characters++;

}

    printf("%d", amount_of_characters);

    return 0;
}
Mutating Algorithm
  • 2,604
  • 2
  • 29
  • 66
  • What exactly is your problem? After leaving the loop the program will terminate as it does not contain other loop. Is the `printf` missing? How do you execute the program and in which environment? – Gerhardh Apr 26 '19 at 05:47

1 Answers1

0

After any stdio function gets an EOF, all future calls to them will also just give EOF, even if there's more content now. This is required by the standard. Luckily, the standard also gives a solution: call clearerr and the EOF flag will be reset.