Dubious C code
Frank Wales
frank at zen.co.uk
Tue Feb 7 04:46:48 AEST 1989
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 <stdio.h>
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 at zen.co.uk<->mcvax!zen.co.uk!frank]
Zengrange Ltd., Greenfield Rd., Leeds, ENGLAND, LS9 8DB. (+44) 532 489048 x217
More information about the Comp.lang.c
mailing list