Newsgroups: comp.std.c Path: utzoo!sq!msb From: msb@sq.sq.com (Mark Brader) Subject: Re: Declarations in switches, errors Message-ID: <1989Oct3.184849.20106@sq.sq.com> Reply-To: msb@sq.com (Mark Brader) Organization: SoftQuad Inc., Toronto References: <561@crdos1.crd.ge.COM> <11158@smoke.BRL.MIL> <637@crdos1.crd.ge.COM> <1989Sep30.052000.13719@utzoo.uucp> <30540@news.Think.COM> Date: Tue, 3 Oct 89 18:48:49 GMT Actually, there *is* a way to "normally enter" the switch body. Instead of entering the body by a jump from the switch header, you put a label on the body itself and jump to that! main() { int k = 0, m = 0; switch (m) bleagh: { int j = 4; case 0: printf ("%d\n", j); } if (k++ == 0) goto bleagh; } Assuming that the implementation doesn't trap on undefined values, this prints a garbage integer value, then 4. It would never have occurred to me to try this, if not for this discussion stream, and I can't dream of a sensible use for it. Isn't C wonderful? Only slightly less bizarre is to put one of the case labels ON the switch body rather than IN it -- 3.6.4.2 allows this and the compilers we have here support it -- whereupon you get initialization IF it is the first case that is chosen. For instance: initj() {printf ("initj() called\n"); return 4;} main() { int k; for (k = 0; k < 3; ++k) switch (k) case 1: { int j = initj(); default: printf ("%d\n", j); } } Assuming that the implementation doesn't trap on undefined values, this prints a garbage integer value, "initj() called", 4, and another garbage integer value. -- Mark Brader "I don't care HOW you format char c; while ((c = SoftQuad Inc., Toronto getchar()) != EOF) putchar(c); ... this code is a utzoo!sq!msb, msb@sq.com bug waiting to happen from the outset." --Doug Gwyn This article is in the public domain.