Weird identifier declaration
David Lai
lai at mips.COM
Fri Aug 25 03:46:20 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)
>
>{
> int a;
>
> a = 3;
>
> (void) sizeof (enum colour {red, green, blue});
>
> printf ("red = %d, green = %d, blue = %d\n", red, green, blue);
>}
>
>The behaviour is similar to :-
>
>int main (void)
>{
> ...
> {
> enum colour {red, green, blue};
>
> printf ( ...
> }
>}
>
>I guess the question is was this intentional ?
>
>(just for record you can also use the following instead of sizeof () :
> a = (enum colour {red, green, blue})3;
>as well !).
>
>Nigel Holder, INMOS Limited | mail[uk]: nigel at inmos.co.uk or ukc!inmos!nigel
>1000 Aztec West, Bristol, UK | [us]: uunet!inmos-c!nigel
>Phone: +44 454 616616 x508 | Internet: @col.hp.com:nigel at inmos-c
In section 3.1.2.1 (scope of identifiers) it states that tags and enum constants
begin just after the appearance of the tag or defining enumerator. Also 3.1.2.1
describes the scope of your examples as 'block' meaning they will persist
until the '}' that ends the block in which they are declared.
In other words you can use the enum constants and types until the end of the
block according to ANSI.
--
"What is a DJ if he can't scratch?" - Uncle Jamms Army
David Lai (lai at mips.com || {ames,prls,pyramid,decwrl}!mips!lai)
More information about the Comp.std.c
mailing list