Path: utzoo!attcan!uunet!lll-winken!ames!vsi1!wyse!mips!lai From: lai@mips.COM (David Lai) Newsgroups: comp.std.c Subject: Re: Weird identifier declaration Message-ID: <26159@gumby.mips.COM> Date: 24 Aug 89 17:46:20 GMT References: <1898@brwa.inmos.co.uk> Reply-To: lai@mips.COM (David Lai) Organization: MIPS Computer Systems, Sunnyvale, CA Lines: 51 In article <1898@brwa.inmos.co.uk> nigel@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@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@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@mips.com || {ames,prls,pyramid,decwrl}!mips!lai)