Newsgroups: comp.lang.c Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!keinstr!chaplin From: chaplin@keinstr.uucp (chaplin) Subject: Re: C puzzle Message-ID: <1991Jun25.210355.591@keinstr.uucp> Organization: Keithley Instruments, Cleveland, Ohio References: <4007@d75.UUCP> Date: Tue, 25 Jun 1991 21:03:55 GMT In article <4007@d75.UUCP> thomas@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@keinstr.uucp / CI$: 76307,3506 / felt I was found when He #include / looked at me with His #include "disclaimer.h" /* cover all bases */ / forgiving eyes." - Michael Card