Understanding the word count program from K&R book
I have started learning C programming using the classic C book by Ritchie & couple of other books as a reference. While going through solved examples I came across this program which counts new lines, characters & words. I understood new line and character part without any issues (obviously). However the logic to count words is confusing me. Can any one please help me in deciphering the code (in blue)?
Code:
#include <stdio.h>
#include <stdlib.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */
int main(void) {
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("line:%d word:%d Character:%d\n", nl, nw, nc);
return(0);
}