Word counting by Kernighan won't compile =(

Mark Jayson Alvarez jay2xra at yahoo.com
Tue Mar 1 05:23:28 GMT 2005

  Sorry for asking this question here. I just thought
that this might be a platform specific issue=). I'm
reading this book(The C Programming Language 2nd
Edition by Brian W. Kernighan. Upon reading the book,
I came up with this example code. It says, it will
count the number of words, lines and characters in the
command line, until I send an EOF signal. I copied it
verbatim and tried compiling it with plain "cc

It exited with errors:

word.c: In function `main':
word.c:17: error: invalid lvalue in assignment

Any idea what this means?


Here's the code:

#define IN      1
#define OUT     0

   int c, nl, nw, nc, state;

   state = OUT;
   nl = nw = nc = 0;
while ((c = getchar()) != EOF) {

  if (c == '\n')
  if (c == ' ' || c == '\n' || c = '\t')
    state = OUT;
  else if (state == OUT) {
     state = IN;

 printf("%d %d %d\n", nl, nw, nc);

Do you Yahoo!? 
Yahoo! Mail - 250MB free storage. Do more. Manage less. 

More information about the freebsd-questions mailing list