Declarations in switches, errors

michael lawley lawley at cs.mu.OZ.AU
Mon Oct 2 13:14:34 AEST 1989


On 1 Oct 89 14:32:00 GMT,
ok at cs.mu.oz.au (Richard O'Keefe) said:

> In article <19907 at mimsy.UUCP>, chris at mimsy.UUCP (Chris Torek) writes:
>> Even a compiler that is only 1/3 decent% will warn about unreachable
>> initialisers.

> I tried the following program (laid out to fit in two lines):
> 	main(argc) int argc; { switch (argc) { int i = 1; double x = 2.0;
>         case 1: case 2: exit(i); default: exit(!!x); }}
> On a Sun-3/50 running SunOS 4.0.something
> 	cc:	"line 1: warning: statement not reached"
> 	CC:	"warning:  statement not reached: case label missing"
> 	lint:	"(1): warning: statement not reached"
> 	gcc:	/* silence */
> 	gcc -W:	/* silence */

I just ran the same code through "gcc -Wall -O".  The -O causes gcc to do
dataflow analysis etc. which it would otherwise not do.  Here is the output:

tt.c: In function main:
tt.c:1: warning: return-type defaults to `int'
tt.c:2: warning: implicit declaration of function `exit'
tt.c:1: warning: `i' may be used uninitialized in this function
tt.c:1: warning: `x' may be used uninitialized in this function
tt.c:2: warning: control reaches end of non-void function

Running with just "gcc -W -O" (for those of you who like sloppy code) gives

tt.c: In function main:
tt.c:1: warning: `i' may be used uninitialized in this function
tt.c:1: warning: `x' may be used uninitialized in this function

> I suppose you *could* call this warning about unreachable initialisers,
> but calling them unreachable *statements* seems a little unhelpful.
> CC's message makes sense because C++ *would* allow a case label before
> the declarations (so the initialisers would be done sometimes!).

These would seem to me to be much more useful error messages than the other
compilers produce.

> Do I have permission to call gcc indecent now? 

What about now?



More information about the Comp.std.c mailing list