K&R C Counting Characters

c, linux

Solution

Whenever you want to stop it just send the EOF signal to the shell.

Ctrl+d in Linux or Ctrl+z on Windows.

By the way (as additional info) Ctrl+c send `SIGINT` to a process in Linux and on Windows it does something similar.

Problem

I'm working out of the K&R book, and I'm on the code example of how to count characters from a stream of text. I copied their code and tried running it, but when the command line prompts you for characters, the loop doesn't exit and thus will never print out the character count. Is there an error here I'm not catching? ``` #include <stdio.h> main() { long nc; nc = 0; while(getchar() != EOF) { ++nc; } printf("%1d\n", nc); } ```

Original source