ANSI, K&R, H&S (was: Re: Just a minor new twist on free())

Chris Torek chris at mimsy.umd.edu
Fri Nov 2 23:35:34 AEST 1990


In article <222 at smds.UUCP> sw at smds.UUCP (Stephen E. Witham) writes:
>The whole point of the project that led to the [H&S] book was to
>seek out the fuzzy edge of C, see what was really happening there,
>and FIGURE OUT HOW TO AVOID IT.  The book is full of advice like, "Don't
>use this feature," or, "Sometimes this doesn't work, sometimes this doesn't 
>work, so the most conservative and portable way to do it seems to be this..."
>
>Stephen Clamage tried to make this same point to Dan Bernstein, but slipped
>up by talking about "portable" and "enums" in the same sentence. ...

Or, to put it another way, if all you have read is K&R I, you might
be tempted to write the following fragment for counting vowels:

#include <stdio.h>

main()
{
	char *p, *getword();
	int anum, enum, inum, onum, unum, wnum, ynum;

	anum = enum = inum = onum = unum = wnum = ynum = 0;
	while ((p = getword()) != NULL) {
		if (*p == 'c' && strcmp(p, "cwm") == 0) {
			/*
			 * This English word was imported from Welsh.
			 * It is probably the only one in which w is
			 * a vowel.
			 */
			wnum++;
			continue;
		}
		for (;;) {
			switch (*p++) {
			case '\0':
				break;
			case 'a':
				anum++;
				continue;
			case 'e':
				enum++;
				continue;
			case 'i':
				inum++;
				continue;
			case 'o':
				onum++;
				continue;
			case 'u':
				unum++;
				continue;
			case 'y':
				ynum++;	/* could be a consonant... */
				continue;
			default:
				continue;
			}
			break;
		}
	}
	/* print out results */
	.
	.
	.

This code will compile on some systems, and not on others, because
`enum' is only sometimes a keyword.

>"The Tao that can be dialed is not a working Tao."

... but if you call this 1-900 number today, at only $1235917/minute ... :-)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.std.c mailing list