nigel@wraxall.inmos.co.uk (Nigel Holder) (08/23/89)
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
lai@mips.COM (David Lai) (08/25/89)
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)
dfp@cbnewsl.ATT.COM (david.f.prosser) (08/25/89)
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) >{ > (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...