switch.and.case

BlandMA mab at druky.UUCP
Fri Jun 1 06:32:25 AEST 1984


x

I see two problems with the switch/case example:

	1. It doesn't even compile on System V because there are duplicate
	   cases in the switch statement.  K&R page 202 prohibits case
	   constants from having the same value.  Also, the line
	   following the switch statement is not reached since there
	   is no "case" preceding it.

	2. The author probably meant to use "case 6" rather than
	   "case '6'".  If this piece of code actually compiles on some
	   compiler, then the "default" case would be taken, instead of
	   the "case 6" that the author probably intended.

A more interesting program that compiles on System V is
shown at the end of this article.  It demonstrates that a case can
jump into the middle of a block (something I never realized, but
is permitted by the K&R language definition).  It's not clear to me
what value j should have the first time - probably undefined.

			Alan Bland
			ihnp4!druky!mab
			AT&T-ISL Denver

main()
{
	int i;

	i=5;
	switch (3) {
	case 1:
		for (i=0; i<10; i++) {
			int j;
			j = i+1;
	case 3:
			printf("i=%d j=%d\n", i, j);
		}
		break;
	}
}

** program output on 3B20 **
i=5 j=0
i=6 j=7
i=7 j=8
i=8 j=9
i=9 j=10



More information about the Comp.lang.c mailing list