Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!xanth!kremer From: kremer@cs.odu.edu (Lloyd Kremer) Newsgroups: comp.lang.c Subject: initialization following switch Keywords: switch,initialize Message-ID: <8348@xanth.cs.odu.edu> Date: 4 Apr 89 20:30:13 GMT Distribution: na Organization: Old Dominion University, Norfolk, Va. Lines: 42 I just fell into a little C language trap, and I thought I would warn others about it. It looks like a very easy mistake to make! (I've been using C almost exclusively for years.) Initializing a variable after the opening brace of a switch does not work. The variable is declared and brought into scope for the remainder of the switch block, but the initialization does not occur. Example: switch(n){ int tmp = 1234; /* * This will declare 'tmp' during the switch but * it will not be set to 1234 ! * It will be garbage!! */ case 1: statement; break; case 2: statement; break; default: break; } I tried this program using both MSC 5.1 and also using UNIX(tm) System V Release 3 (did I really mention those two in one breath?), and in neither case did initialization take place. MSC said nothing. Lint caught it as 'statement not reached'. Is this the pANS conformant behavior? I'm sure it is, but I don't have a copy to verify it. Lloyd Kremer Brooks Financial Systems {uunet,sun,...}!xanth!brooks!lloyd