Path: utzoo!attcan!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!munnari.oz.au!csc.anu.oz.au!csc3.anu.oz.au!csc.canberra.edu.au!news From: eyal@echo.canberra.edu.au (Eyal Lebedinsky) Newsgroups: comp.lang.c Subject: Re: switch vs. initializing declarations Summary: Flow control in C. Keywords: switch Message-ID: <1990Sep16.081828.29477@csc.canberra.edu.au> Date: 16 Sep 90 08:18:28 GMT References: <1990Sep14.204028.21189@ingres.Ingres.COM> <1990Sep16.005308.8804@chinet.chi.il.us> Sender: news@csc.canberra.edu.au Organization: none Lines: 48 >In article <1990Sep14.204028.21189@ingres.Ingres.COM> jeff@ingres.com (Jeff Anton) writes: >A few days ago, it occured to me that I didn't have a good feeling >as to what the following code fragment which seems to be legal C >means. This is a retorical question and is not real world code, but I >would like to hear from someone who has a good knowledge of the >formal C specifications. Please reply to me personally as I don't >often read comp.lang.c but post to comp.lang.c if you wish. > >main(argc, argv) >int argc; >char *argv[]; >{ > switch (argc) { > int v = 1; > > default: > v += 5; > case 1: > printf("%d\n", v); > } > return 0; >} > > >The ambiguity is whether or not 'v' should be initialized or not. >All compilers I've tested recognize the declaration but do not >do the initialization. Some report line 6 statement not reached when >clearly the statement does have the declaritoy effect.... > the 'switch' is like any goto. If you enter a block NOT through the beginning then you cannot trust initialising code. It is generaly not a good thing to do, and in the 'switch' statement you NEVER enter at the begining, so don't initialize . If you insist on having an initialized 'v = 1' then make it external to the 'switch', maybe: { int v = 1; switch (....) { ... } return 0; } Regards Eyal -- Regards Eyal