initialization following switch

Lloyd Kremer kremer at cs.odu.edu
Wed Apr 5 06:30:13 AEST 1989


	I just fell into a little C language trap, and I thought I would warn
	others about it.  It looks like a very easy mistake to make!
	(I've been using C almost exclusively for years.)

	Initializing a variable after the opening brace of a switch does not
	work.  The variable is declared and brought into scope for the
	remainder of the switch block, but the initialization does not occur.

	Example:

		switch(n){
		    int tmp = 1234;

		    /*
		     *   This will declare 'tmp' during the switch but
		     *   it will not be set to 1234 !
		     *   It will be garbage!!
		     */

		case 1:
		    statement;
		    break;
		case 2:
		    statement;
		    break;
		default:
		    break;
		}


	I tried this program using both MSC 5.1 and also using
	UNIX(tm) System V Release 3 (did I really mention those two in
	one breath?), and in neither case did initialization take place.
	MSC said nothing.  Lint caught it as 'statement not reached'.

	Is this the pANS conformant behavior?  I'm sure it is, but I don't
	have a copy to verify it.

					Lloyd Kremer
					Brooks Financial Systems
					{uunet,sun,...}!xanth!brooks!lloyd



More information about the Comp.lang.c mailing list