Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 2 Mar 2005 02:15:07 +0100
From:      Luyt <luyt@ovosoft.nl>
To:        freebsd-questions@freebsd.org
Subject:   Re: Word counting by Kernighan won't compile =(
Message-ID:  <200503020215.07877.luyt@ovosoft.nl>
In-Reply-To: <20050301052323.29837.qmail@web51610.mail.yahoo.com>
References:  <20050301052323.29837.qmail@web51610.mail.yahoo.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tuesday 01 March 2005 06:23, Mark Jayson Alvarez wrote:

>   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
> myprog.c"

#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);
}


Compile & sample output:

luyt@Deschutes /home/luyt % gcc wc.c -o wc
luyt@Deschutes /home/luyt % ./wc <wc.c
24 73 349


-- 

"The ability of the OSS process to collect and harness 
the collective IQ of thousands of individuals across 
the Internet is simply amazing." - Vinod Vallopillil
http://www.opensource.org/halloween/halloween4.php



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200503020215.07877.luyt>