A good use of a bad feature

Jim Williams jim at umcp-cs.UUCP
Sat May 3 14:26:28 AEST 1986


While discussing various features and miss-features of C
with my friend Charley (mangoe) Wingate recently, we both
agreed that the fall through case statement is among the least
defensible of C's features.  I submit the program below as the
best use I have ever found for this feature.

--------------- CUT HERE ------------
/*
 * xmas.c - a program to print The Twelve Days of Christmas
 * using the C fall thru case statement.
 * If this wasn't my idea, I appologize to whomever I
 * got the idea from, but I wrote the program 5 years
 * ago and I don't remember now. 
 *
 * Jim Williams, jim at maryland, 2 May 1986
 */

/* 
 * If you have an ANSI compatible terminal then
 * #define ANSITTY.  It makes the five Golden rings 
 * especially tacky.
 */

#define ANSITTY

#include <stdio.h>

char *day_name[] = {
	"",
	"first",
	"second",
	"third",
	"fourth",
	"fifth",
	"sixth",
	"seventh",
	"eighth",
	"ninth",
	"tenth",
	"eleventh",
	"twelfth"
	};


main()
{
	int day;

	printf("The Twelve Days of Christmas.\n\n");

	for (day=1; day<=12; day++) {
		printf("On the %s day of Christmas, my true love gave to me\n",
			day_name[day]);

		switch (day) {
		case 12:
			printf("\tTwelve drummers drumming,\n");
		case 11:
			printf("\tEleven lords a leaping,\n");
		case 10:
			printf("\tTen ladies dancing,\n");
		case 9:
			printf("\tNine pipers piping,\n");
		case 8:
			printf("\tEight maids a milking,\n");
		case 7:
			printf("\tSeven swans a swimming,\n");
		case 6:
			printf("\tSix geese a laying,\n");
		case 5:
#ifdef ANSITTY
			printf("\tFive [1;5;7mGolden[0m rings,\n");
#else
			printf("\tFive Golden rings,\n");
#endif
		case 4:
			printf("\tFour calling birds,\n");
		case 3:
			printf("\tThree French hens,\n");
		case 2:
			printf("\tTwo turtle doves, and\n");
		case 1:
			printf("\tA partridge in a pear tree.\n\n");
		}
	}
}
-- 
Jim \/\/illiams
jim at mimsy.umd.edu
umcp-cs!jim.UUCP



More information about the Comp.lang.c mailing list