I must say that I'm just starting with C Programming. I've had the urge to do so and decided to. Prior to this however, I was a complete BASIC programmer, which makes it a little hard for me to understand some things.
K&R's »The C Progamming Language« is the book that I have decided to use (I've tried Bruce Eckel's »Thinking In C« before, but nevertheless thought this might clear some problems)
Chapter 1.5.4 »Counting Words« has a program that I found hard to understand:
Code:
#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);
}
I cannot understand how this works. How does the while loop allow the number of words to be counted? I don't see c having any index operations on it.
Bashing is accepted humbly
Thank you!