hi I'm reading a book called the c programming language second edition by dennis ritchie and one of the source codes in section 1.5.4 doesn't work properly.

here's the code:
Code:
#include <stdio.h>

#define IN 1    /* inside a word */
#define OUT 0   /* outside a word */

/* count lines, words, and characters in input */
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);
}
nc increments correctly but also reads '\n' as character too.
also the main problem is that nw never increases from zero no matter how many words i type. and I'm just beginner i don't know how to debug the code.

ps: I've tried the code on two operating systems both had this problem (ubuntu with gcc and windows with mingw)