switch vs. initializing declarations
John Mundt
john at chinet.chi.il.us
Sun Sep 16 10:53:08 AEST 1990
In article <1990Sep14.204028.21189 at ingres.Ingres.COM> jeff at 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....
I'm surprised it compiles, but it does. Line 6 is not reached because
it is not within any of the case statements. Therefore, there is no
argument you can give to argc which will reach the "case" of
int v = 1; so it is never executed.
It runs probably because most compilers assign a type of int to
variables and functions not specifically declared. lint
has this to say about the program:
warning: statement not reached
(6)
--
---------------------
john at admctr.chi.il.us
John Mundt Teachers' Aide, Inc. P.O. Box 1666, Highland Park, IL
(708) 998-5007 || -432-8860
More information about the Comp.lang.c
mailing list