Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: [ On topic, amazingtly ]: labels and declarations Date: Sun, 17 Mar 2019 16:49:52 -0700 Organization: None to speak of Lines: 54 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="dffa6b7d2dd8d0122e38bd70d28a7fdc"; logging-data="32173"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pMZz69gO8Zt07FNLtcJlo" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:E0ZIlYneUhb0RhX9KLGlpWSB91Q= sha1:Vcxlbbe6WLs4hqbi/9TSLfDK/sk= Xref: csiph.com comp.lang.c:142005 Lew Pitcher writes: > Keith Thompson wrote: >> Richard Damon writes: >> [...] >>> This unfortunately leads to the following being grammatically correct: >>> >>> if(flag) int x; >>> >>> which would mean that the following code might be valid or not depending >>> of if flag was true at run time, or >>> >>> if(flag) int x; >>> else float x; >>> >>> where the type of x changes at run time, or even >> [...] >> >> C++ allows this; a declaration (if it appears inside a function >> defintion) is a kind of statement. The scope of x is limited to the >> declaration-statement, so the above is equivalent to: >> >> if (flag) { int x; } >> else { float x; } >> >> which is valid in C. > > By implication, even current C supports this, afaict. Consider > > if (!flag) goto label1; > int x; > goto label2; > label1: ; > float x; > label2: ; > > Doesn't this accomplish the same end-state as Richard's > if(flag) int x; > else float x; > ? No. In your example with gotos, the declaration `float x;` is illegal, because there's already an `x` defined in the same scope. In Richard's example, if the scopes of the two `x`s extended beyond the if-else statement, it would be difficult to define the semantics or, more likely, the second declaration would be illegal for the same reason. If the scopes were defined as they are in C++, they would be non-overlapping and unambiguous (and not useful, since nothing could refer to either `x`). -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */