frank@zen.co.uk (Frank Wales) (02/07/89)
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@zen.co.uk<->mcvax!zen.co.uk!frank]
Zengrange Ltd., Greenfield Rd., Leeds, ENGLAND, LS9 8DB. (+44) 532 489048 x217 gwyn@smoke.BRL.MIL (Doug Gwyn ) (02/09/89)
In article <1508@zen.UUCP> frank@zen.co.uk (Frank Wales) writes: >The question is, *should* it compile without any complaints at all? Why not? It's legitimate C code. It follows the rules. So what if it's a stupid algorithm? It's not the compiler's job to criticize the choice of algorithms.
alanm@cognos.uucp (Alan Myrvold) (02/11/89)
In article <9619@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes: >In article <1508@zen.UUCP> frank@zen.co.uk (Frank Wales) writes: >>The question is, *should* it compile without any complaints at all? > >Why not? It's legitimate C code. It follows the rules. >So what if it's a stupid algorithm? It's not the compiler's >job to criticize the choice of algorithms. I didn't realize that a CASE statement within the body of a FOR loop was legitimate C code, and just a stupid algorithm. Curiously enough, although it compiles without complaints and generates the same output on UNIX cc, UNIX gcc, PC-DOS Turbo C, IBM MVS/TSO SAS C, and IBM MVS/TSO IBM C (109 Bananas) ... VAX/VMS cc does have the sense to complain : $ cc rainbow.c case 1: %CC-E-NOTSWITCH, Default labels and case labels are valid only in "switch" statements. At line number 17 in PATH$USR:[USR.ALANM]RAINBOW.C;1. } %CC-I-NOBJECT, No object file produced. At line number 25 in PATH$USR:[USR.ALANM]RAINBOW.C;1. %CC-I-SUMMARY, Completed with 1 error(s), 0 warning(s), and 1 informational messages. At line number 26 in PATH$USR:[USR.ALANM]RAINBOW.C;1. %CC-I-NOBJECT, No object file produced. At line number 26 in PATH$USR:[USR.ALANM]RAINBOW.C;1. $ --- Alan Myrvold 3755 Riverside Dr. uunet!mitel!sce!cognos!alanm Cognos Incorporated P.O. Box 9707 alanm@cognos.uucp (613) 738-1440 x5530 Ottawa, Ontario CANADA K1G 3Z4
krs0@bunny.UUCP (Rod Stephens) (02/11/89)
Very interesting. I tried it on my Ultrix system and it gave veeerrryyy
interesting results. I tried it under VMS and it gave a
Default labels and case labels are valid only in "switch" statements.
Apparently it objected to the "case 1" after the for statement. When I
changed it so the puts("Bananas!"); was also inside the case 2 it worked
in the expected (boring) way.gwyn@smoke.BRL.MIL (Doug Gwyn ) (02/16/89)
In article <5251@cognos.UUCP> alanm@cognos.UUCP (Alan Myrvold) writes: >I didn't realize that a CASE statement within the body of a FOR loop >was legitimate C code, and just a stupid algorithm. Surprise! >VAX/VMS cc does have the sense to complain : >%CC-E-NOTSWITCH, Default labels and case labels are > valid only in "switch" statements. That compiler is broken -- the case label WAS inside a switch body. I admit that making use of this language feature is almost always ill-advised (and, apparently, not portable to VAX/VMS). "Duff's device" makes use of it in an essential way, though..