Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!nrl-cmf!ukma!tut.cis.ohio-state.edu!ucbvax!decwrl!sun!pitstop!sundc!seismo!uunet!mcvax!ukc!etive!hwcs!zen!frank From: frank@zen.co.uk (Frank Wales) Newsgroups: comp.lang.c Subject: Dubious C code Keywords: for switch in case do bananas Message-ID: <1508@zen.UUCP> Date: 6 Feb 89 18:46:48 GMT Organization: Zengrange Limited, Leeds, England Lines: 44 A colleague of mine [honest, it wasn't me] was in the process of chopping some code around, and accidentally got the equivalent of the following section of code past the C compiler without any trouble. It lints completely clean, too. The question is, *should* it compile without any complaints at all? A case can be made (sic) by analogy to statement labels, but I think the compiler should have enough semantic smarts to say, "waaaait a minute, pal!" Have a look yourself: .................................. rainbow.c .................................. #include main() { int zippy,bungle,george=99; for (zippy=0;zippy<4;++zippy) { switch(zippy) { case 0: bungle = 0; break; case 2: for (george=10;george>bungle;--george) /* falls through */ case 1: (void)puts("Bananas!"); break; default: (void)puts("\"Time for bed,\" said Geoffrey."); } } return(0); } ............................................................................... [System: HP-9000 model 840 rel 2.1 -- lint is pcc-based, compiler is not.] Now, guess how many times "Bananas!" should get printed. Please write the answer on a postcard and put it in a drawer. :-) -- Frank Wales, Systems Manager, [frank@zen.co.uk<->mcvax!zen.co.uk!frank] Zengrange Ltd., Greenfield Rd., Leeds, ENGLAND, LS9 8DB. (+44) 532 489048 x217