As a small background, I'm fairly new to the C programming language, and as such I tried to complete some of the exercises in the second edition of Kernighan and Ritchie's tutorial. I understand that I could possibly deal with some problems in more detail using the standard library, but I try to keep my repertoire of useful commands in sync with the book as much as possible.
If that matters, I compile my source in Windows XP using the Tiny C Compiler (TCC) and execute the binaries in the XP console (cmd.exe).
Problem : Processing End-of-File (EOF) characters . I gave a small test case to illustrate the problem. The program seems to handle the character of the EOF (partially). I will try to demonstrate a problem with examples of inputs / outputs.
#include <stdio.h> int main() { int character, count; character = 0; character = getchar(); for (count = 0; character != EOF; ++count) { character = getchar(); } printf("Count: %d", count); return 0; }
Entry example 1: abcd^Z[enter] (where ^ Z / CTRL + Z represents the EOF character, and [enter] represents the Enter key.)
Output Example 1: Count: 4 (expects more input or ends with ^ C / ^ Z [enter])
Input Example 2: abcd^Zefgh
Output Example 2: Count: 4 (expects more input or ends with ^ C / ^ Z [enter])
As noted in both examples, the number of characters is not output until the sequence ^ C / ^ Z [enter] begins. Before initiation, the program expects (and does process) more input. However, as noted in Example 2, when the program encounters the original ^ Z, it stops processing this input line, waiting for more input or returns the correct counter if the sequence ^ C / ^ Z [input] is initiated.
I canโt understand why the program only partially processes the EOF character. It seems to me that if it cuts off the end of sample 2, it should also completely exit the loop. Any ideas why, when recognizing the EOF character, the program does not immediately display the current counter and exit?