C puzzle

chaplin chaplin at keinstr.uucp
Wed Jun 26 07:03:55 AEST 1991


In article <4007 at d75.UUCP> thomas at advent.uucp () writes:
>
>	The following is an interesting 'C' puzzle. 
>	Consider this typical switch statement, typical except that 
>	'default' is misspelled 'defalut'! What is interesting is that
>	any C compiler will not and should not give you a syntax error. 
>	Why?  
>
>
>	switch (cmd)
>	{
>		case GO:
>			...
>			break;
>		case STOP:
>			...
>			break;
>		defalut:
        ^^^^^^^^
This is a valid statement label, useful as the target of a goto statement.
_K&R_ 2nd Edition page 66.
>			printf ("Unknown command\n");
>			break;
>	}
>
>	(Hint: It is bad programming practice to jump into the 
>	middle of a switch statement.)
>
>thomas

That was almost too easy.  BTW, according to _K&R_ it is bad programming
practice in almost all cases to use goto, and since the *only* use of a label
is as the target of the goto statement (_K&R_ 2nd ed. page 222), labels are
almost always bad programming practice.

You did not intend this as a label though.  Hmmm... an interesting problem.
I'm not sure how much help lint would be, but I do know that Saber C would
detect this as a label defined but never referenced.

-- 
Roger Chaplin / Instruments Division Engineering / "Though I'd been lost now I
chaplin at keinstr.uucp / CI$: 76307,3506          / felt I was found when He
#include <disclaimer.h>                        / looked at me with His
#include "disclaimer.h" /* cover all bases */ / forgiving eyes." - Michael Card



More information about the Comp.lang.c mailing list