[comp.std.c] Scope of switch statements

gwyn@smoke.BRL.MIL (Doug Gwyn) (11/09/89)

In article <15743@bloom-beacon.MIT.EDU> tada@athena.mit.edu (Michael J Zehr) writes:
>is this obfuscated code not ANSI or is the compiler broken?  

The compiler is broken.

henry@utzoo.uucp (Henry Spencer) (11/10/89)

In article <15743@bloom-beacon.MIT.EDU> tada@athena.mit.edu (Michael J Zehr) writes:
>is this obfuscated code not ANSI or is the compiler broken?  

It is legal, although ugly, ANSI C.  Also legal (but ugly) traditional C.
Dennis Ritchie himself has officially blessed this disgusting construct,
and indeed it has one or two legitimate uses.
-- 
A bit of tolerance is worth a  |     Henry Spencer at U of Toronto Zoology
megabyte of flaming.           | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

exspes@gdr.bath.ac.uk (P E Smee) (11/14/89)

In article <1989Nov9.200639.8868@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>In article <15743@bloom-beacon.MIT.EDU> tada@athena.mit.edu (Michael J Zehr) writes:
>>is this obfuscated code not ANSI or is the compiler broken?  
>
>It is legal, although ugly, ANSI C.  Also legal (but ugly) traditional C.
>Dennis Ritchie himself has officially blessed this disgusting construct,
>and indeed it has one or two legitimate uses.

Can you point me at a reference in 'The C programming language' (or in
any other reference that I'm likely to be able to find)?  I'm *not*
doubting you, but I *would* like to know exactly what it is supposed
to mean, then.  In particular, if you branch (via the switch) to one
of the cases inside the {}'s of the for, are you then under control of
the for?  That is, do you iterate the for or do you fall out of the
bottom bracket on the first go-through?

-- 
 Paul Smee               |    JANET: Smee@uk.ac.bristol
 Computer Centre         |   BITNET: Smee%uk.ac.bristol@ukacrl.bitnet
 University of Bristol   | Internet: Smee%uk.ac.bristol@nsfnet-relay.ac.uk
 (Phone: +44 272 303132) |     UUCP: ...!uunet!ukc!gdr.bath.ac.uk!exspes

peter@guardian.UUCP (peter) (11/15/89)

In article <1989Nov9.200639.8868@utzoo.uucp> henry@utzoo.uucp (Henry Spencer)
writes:
|In article <15743@bloom-beacon.MIT.EDU> tada@athena.mit.edu (Michael J Zehr)
|writes:
|>There was a recent article in comp.lang.c on obfuscated use of switches
|>and labels mixed together which i decided to try to compile using DEC's
|>VAX C (VMS) compiler:
|>#include <stdio.h>
|>main() {
|>  int i, j=1, k=1;
|>  for(i=0; i<=2; ++i) 
|>    switch(i) {
|>      case 0: for(; ++k % 3; ++j) {
|>      case 1:   printf(" j = %d,", j);
|>      case 2:   printf(" k = %d,", k);
|>              }
|>    }
|>}
|>
|>(i've reduced the code to just the section i have a question concerning)
|>
|>on the '1' and '2' case lines, the compiler gives an error "case labels
|>are valid only in "switch" statements."
|>
|>is this obfuscated code not ANSI or is the compiler broken?  
|
|It is legal, although ugly, ANSI C.  Also legal (but ugly) traditional C.
|Dennis Ritchie himself has officially blessed this disgusting construct,
|and indeed it has one or two legitimate uses.

(Michael Zehr's original posting added for clarity)

I've stared at this and can't make sense of it.  Could someone provide me
with enlightenment?
-------------------------------------------------------------------------------
Peter Plamondon, Intel Corp, 5200 NE Elam Young Pkwy, Hillsboro, OR  97124-6497
Internet: peter@langlab1.hf.intel.com                           +1 503-696-5219
UUNET   : uunet!intelhf!langlab1!peter     "I speak for myself, as best I can."
UUCP    : tektronix!psueea!foobar!langlab1.hf.intel.com!peter    
-------------------------------------------------------------------------------

henry@utzoo.uucp (Henry Spencer) (11/16/89)

In article <301@guardian.UUCP> peter@langlab1.hf.intel.com (Peter Plamondon) writes:
>|>    switch(i) {
>|>      case 0: for(; ++k % 3; ++j) {
>|>      case 1:   printf(" j = %d,", j);
>|>      case 2:   printf(" k = %d,", k);
>|>              }
>|>    }
>
>I've stared at this and can't make sense of it.  Could someone provide me
>with enlightenment?

Rewrite it this way:

	if (i == 0)
		goto zero;
	else if (i == 1)
		goto one;
	else if (i == 2)
		goto two;
	else
		goto done;
zero:	for ... {
		one:	printf...;
		two:	printf...;
	}
done:

Yes, branching into a loop is disgusting, but it is legal.

Don't ask me what the code is supposed to *do*.
-- 
A bit of tolerance is worth a  |     Henry Spencer at U of Toronto Zoology
megabyte of flaming.           | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

gwyn@smoke.BRL.MIL (Doug Gwyn) (11/16/89)

In article <1989Nov14.104203.26192@gdt.bath.ac.uk> exspes@gdr.bath.ac.uk (P E Smee) writes:
>In particular, if you branch (via the switch) to one of the cases
>inside the {}'s of the for, are you then under control of the for?

In C you may always branch to a label within a control construct such
as the body of a "for" statement, whether or not a "switch" is involved.
The associated semantics are fairly obvious if you grok the notions of
labels, identifier scopes, and initialization of variables.