Declarations in switches, errors

Lloyd Kremer kremer at cs.odu.edu
Thu Sep 28 03:10:18 AEST 1989



In article <561 at crdos1.crd.ge.COM> davidsen at crdos1.crd.ge.COM (Wm E Davidsen Jr)writes:

>main() {
>  int i = 2;
>  switch (i) {
>  int j = 4;
>  case 1: j += 4; break;
>  case 2:
>  case 3:
>  case 'a':
>  case 'b':
>  j--; break;
>  }
>}
>
>Note the initialization in the 4th statement. I was unable to find any
>compiler which generated working code for this initialization (although
>one did complain that the code was not reached). I tried Sun, Ultrix,
>Xenix and gcc compilers.
>
>This is an error in the compilers! As ugly as this is, the ANSI standard
>(3.1.2.4) says "If an initialization is specified for the value stored
>in the object, it is performed on each normal entry, but not if the
>block is entered by a jump to a label."
>
>I submit that executing a switch statement constitutes "normal entry"


No, a switch statement is entered by a jump to a label.  The jump is to
any one of several places depending on which "case" is true, but an automatic
initialization at the start of a switch statement is never performed.  The
variable is brought into scope within the switch block, but the initial
contents of the variable are garbage.

-- 
					Lloyd Kremer
					...!uunet!xanth!kremer
					Have terminal...will hack!



More information about the Comp.std.c mailing list