I read the book of K&R "The C Programming Language (Second Edition)", in the part 1.5.4 (words counting), it shows a programm(See the end).
This program is OK, but I don't know how the if-else sentence works, see, the 1st if contains c == '\n', the 2nd also contains '\n', and once the 2nd "if" do the "state == IN", will the last else if work?
So anyway, it's different from the "if-else" I learned before, I just know that all the "if" conditions should be different, and computer will execute only one sentence. So how this program run? Thanx for answer.
P.S: the program
Code:
#include <stdio.h>
#define IN 1
#define OUT 0
main()
{
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("%d %d %d\n", nl, nw, nc);
}