Path: utzoo!attcan!uunet!ginosko!xanth!kremer From: kremer@cs.odu.edu (Lloyd Kremer) Newsgroups: comp.std.c Subject: Re: Declarations in switches, errors Summary: The compilers are correct Message-ID: <10041@xanth.cs.odu.edu> Date: 27 Sep 89 17:10:18 GMT References: <561@crdos1.crd.ge.COM> Organization: Old Dominion University, Norfolk, Va. Lines: 40 In article <561@crdos1.crd.ge.COM> davidsen@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!