I picked up the "C Programming Language" and have been learning it bit by bit but Im alittle confused about EOF. I example 1-8, we need to write a program that outputs the number of tabs, blanks, and newlines entered into the program, using a while loop to control when the program performs the outputs. I know that EOF is a negative value (-1 apparently), but how do I enter it into the program so that the while loop ends since every keystroke is interpretted as a character. Here's the code:
Code:
#include <stdio.h> main(){ int c, tab = 0, blank = 0, nl = 0; printf("EOF = %d", EOF); printf("\n\n"); while ((c = getchar()) != EOF) if (c == '\n') ++nl; else if(c == '\t') ++tab; else (c == ' '); ++blank; printf("\nNew Lines: %d", nl); printf("\nTabs: %d", tab); printf("\nBlanks: %d", blank); }
Comment