Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #143045
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Scopes of identifiers |
| Date | 2019-04-11 17:08 -0700 |
| Organization | None to speak of |
| Message-ID | <lny34g13xb.fsf@kst-u.example.com> (permalink) |
| References | <098da5fe-31d3-47b7-b695-d4faea10b366@googlegroups.com> <q8o4h9$dqe$1@dont-email.me> <5b59a4e9-81d3-4442-a9e6-6fa8419a08e7@googlegroups.com> <q8odkv$60b$1@dont-email.me> |
Andrey Tarasevich <andreytarasevich@hotmail.com> writes:
> On 4/11/2019 2:20 PM, James Kuyper wrote:
>> On Thursday, April 11, 2019 at 3:28:49 PM UTC-4, Andrey Tarasevich wrote:
[...]
>>> ... and thus allow declarations of
>>> identical identifiers. For example
>>>
>>> for (int i = 0; i < 10; ++i)
>>> {
>>> int i = 42;
>>> }
>>>
>>> In this case the inner `i` has its own nested scope and does not
>>> conflict with `i` from the cycle header.
>>
>> I believe this is incorrect, though I can't test it with a compiler
>> right now. The scope of the first 'i' ends with the '}' at the end of
>> the loop; the same is true of the inner 'i'.
>
> This code is perfectly correct and actually fully agrees with what you
> _yourself_ were stating above.
>
>> "Two identifiers have the _same scope_ if and only if their scopes
>> terminate at the same point." (6.2.1p6). The phrase "same scope" is
>> italicized, an ISO convention indicating that this clause is considered
>> to be the official definition of the italicized phrase.
>
> No. Nested blocks mean nested scopes in C. It does not matter whether
> the block is "explicit" (a pair of `{}`) or "implicit" (as described
> above). This immediately means that scopes associated with nested blocks
> have different termination points.
What are these two different termination points?
Here's the code again:
for (int i = 0; i < 10; ++i)
{
int i = 42;
}
There are two blocks here, one for the for statement and one for its
body. N1570 6.8.5p5:
An iteration statement is a block whose scope is a strict
subset of the scope of its enclosing block. The loop body is
also a block whose scope is a strict subset of the scope of
the iteration statement.
One scope starts at the "for" keyword and ends at the "}" symbol.
The other scope starts at the "{" symbol and ends at the "}" symbol.
They start at different points, but they terminate at the same point.
> There's no difference between the above example and simple
>
> {
> int i = 42;
> {
> int i = 5;
> }
> }
>
> I hope you are not claiming that these two scopes "terminate at the same
> point" in this case.
The difference is that in this case the two scopes terminate at
different points, the two "}" symbols, so they are not the "same scope".
I'd like to be wrong about this. In the first example, I'd *like*
the declaration of the inner i, shadowing the outer i, to be legal
(though it's poor style). At least gcc, clang, and tcc accept it,
and I would have assumed it was valid. But given the somewhat
counterintuitive definition of "same scope", I'd say that in the
first example both i's have the same scope, and therefore the code
is invalid.
Do you argue that the two scopes don't end at the same point? If so,
what exactly is a "point"?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Scopes of identifiers Thiago Adams <thiago.adams@gmail.com> - 2019-04-10 16:15 -0700
Re: Scopes of identifiers Bart <bc@freeuk.com> - 2019-04-11 00:33 +0100
Re: Scopes of identifiers Thiago Adams <thiago.adams@gmail.com> - 2019-04-10 16:45 -0700
Re: Scopes of identifiers Thiago Adams <thiago.adams@gmail.com> - 2019-04-10 18:31 -0700
Re: Scopes of identifiers Thiago Adams <thiago.adams@gmail.com> - 2019-04-10 16:34 -0700
Re: Scopes of identifiers Robert Wessel <robertwessel2@yahoo.com> - 2019-04-10 20:50 -0500
Re: Scopes of identifiers James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-04-10 20:02 -0700
Re: Scopes of identifiers Keith Thompson <kst-u@mib.org> - 2019-04-10 20:58 -0700
Re: Scopes of identifiers luser droog <luser.droog@gmail.com> - 2019-04-10 21:48 -0700
Re: Scopes of identifiers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2019-04-11 11:57 -0700
Re: Scopes of identifiers scott@slp53.sl.home (Scott Lurndal) - 2019-04-11 19:12 +0000
Re: Scopes of identifiers Thiago Adams <thiago.adams@gmail.com> - 2019-04-11 12:35 -0700
Re: Scopes of identifiers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-12 01:11 +0100
Re: Scopes of identifiers Thiago Adams <thiago.adams@gmail.com> - 2019-04-12 05:29 -0700
Re: Scopes of identifiers Thiago Adams <thiago.adams@gmail.com> - 2019-04-12 05:39 -0700
Re: Scopes of identifiers Keith Thompson <kst-u@mib.org> - 2019-04-11 14:00 -0700
Re: Scopes of identifiers Nick Bowler <nbowler@draconx.ca> - 2019-04-15 19:48 +0000
Re: Scopes of identifiers Bart <bc@freeuk.com> - 2019-04-11 10:36 +0100
Re: Scopes of identifiers Bart <bc@freeuk.com> - 2019-04-11 14:25 +0100
Re: Scopes of identifiers Keith Thompson <kst-u@mib.org> - 2019-04-11 10:20 -0700
Re: Scopes of identifiers Thiago Adams <thiago.adams@gmail.com> - 2019-04-11 05:00 -0700
Re: Scopes of identifiers Thiago Adams <thiago.adams@gmail.com> - 2019-04-11 06:42 -0700
Re: Scopes of identifiers James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-04-11 08:11 -0700
Re: Scopes of identifiers BGB <cr88192@gmail.com> - 2019-04-10 22:35 -0500
Re: Scopes of identifiers Keith Thompson <kst-u@mib.org> - 2019-04-10 22:04 -0700
Re: Scopes of identifiers BGB <cr88192@gmail.com> - 2019-04-11 05:46 -0500
Re: Scopes of identifiers John Bode <jfbode1029@gmail.com> - 2019-04-11 12:04 -0700
Re: Scopes of identifiers James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-04-11 13:18 -0700
Re: Scopes of identifiers Keith Thompson <kst-u@mib.org> - 2019-04-11 14:31 -0700
Re: Scopes of identifiers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2019-04-11 12:28 -0700
Re: Scopes of identifiers Thiago Adams <thiago.adams@gmail.com> - 2019-04-11 12:50 -0700
Re: Scopes of identifiers James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-04-11 14:20 -0700
Re: Scopes of identifiers Thiago Adams <thiago.adams@gmail.com> - 2019-04-11 14:43 -0700
Re: Scopes of identifiers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2019-04-11 15:04 -0700
Re: Scopes of identifiers Keith Thompson <kst-u@mib.org> - 2019-04-11 17:08 -0700
Re: Scopes of identifiers Andrey Tarasevich <andreytarasevich@hotmail.com> - 2019-04-11 18:45 -0700
Re: Scopes of identifiers James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-04-11 23:16 -0400
Re: Scopes of identifiers Keith Thompson <kst-u@mib.org> - 2019-04-11 20:19 -0700
Re: Scopes of identifiers Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-04-12 14:39 -0700
Re: Scopes of identifiers Ike Naar <ike@sdf.lonestar.org> - 2019-04-13 20:02 +0000
Re: Scopes of identifiers James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-04-11 23:26 -0400
csiph-web