Weird identifier declaration

david.f.prosser dfp at cbnewsl.ATT.COM
Fri Aug 25 03:53:30 AEST 1989


In article <1898 at brwa.inmos.co.uk> nigel at inmos.co.uk () writes:
>Reading the ANSI spec closely I found the following weird case that
>enables the declaration of identifiers *after* assignment statements
>within the same block by using 'sizeof (type-name)'.
>The best way to illustrate this is with an example :-
>
>int main (void)
>{
>	(void) sizeof (enum colour {red, green, blue});
>	printf ("red = %d, green = %d, blue = %d\n", red, green, blue);
>}
>
>I guess the question is was this intentional ?

The charter for X3J11 was "to codify existing practice wherever unambiguous"
[FORWARD, page v].  As this has been the behavior of most implementations,
the answer was clear.

Besides, what other reasonable ruling could be made for this sort of code?
Disallow it, without disallowing all "reasonable" uses for casts and sizeof?
Allow it, but define another form of scope that extends to the end of the
type-name, or the expression statement, or somewhere in between?

>(just for record you can also use the following instead of sizeof () :
>	a = (enum colour {red, green, blue})3;
>as well !).

True enough.  Any expression that includes the possibility of a type
specifier allows for extra declarations.  This only includes casts and
sizeof.

Dave Prosser	...not an official X3J11 answer...



More information about the Comp.std.c mailing list