Declarations in switches, errors

Richard O'Keefe ok at cs.mu.oz.au
Sat Sep 30 18:25:04 AEST 1989


In article <561 at crdos1.crd.ge.COM> davidsen at crdos1.UUCP (bill davidsen) writes:
: In the process of looking for a totally diferent problem, I generated
: the following program:
: main() {
:  int i = 2;
:  switch (i) {
:   int j = 4;
:  case 1: j += 4; break;
[much deleted]

C has *always* worked like this.  The body of a switch is a statement
and the switch is a jump.  Here's a really ugly piece of code I wrote
to illustrate this once:
    main(argc)
	{
	    switch (argc)
		if (0) case 1: printf("1\n");
                else if (0) default: printf("not 1\n");
		else printf("impossible\n");
	    exit(0);
	}
"gcc -ansi -pedantic" still likes it!  If you want initialised declarations,
put the switch inside the block instead of the block inside the switch.
A good C compiler ought to print a warning message about initialised
declarations in a switch() body, but that's "quality of implementation".



More information about the Comp.std.c mailing list