Declarations in switches, errors
Mark Brader
msb at sq.sq.com
Wed Oct 4 04:48:49 AEST 1989
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 at sq.com bug waiting to happen from the outset." --Doug Gwyn
This article is in the public domain.
More information about the Comp.std.c
mailing list