Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #143063

Re: Scopes of identifiers

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Scopes of identifiers
Date 2019-04-12 14:39 -0700
Organization A noiseless patient Spider
Message-ID <86imvij43c.fsf@mailhub.linuxsc.com> (permalink)
References (1 earlier) <q8o4h9$dqe$1@dont-email.me> <5b59a4e9-81d3-4442-a9e6-6fa8419a08e7@googlegroups.com> <q8odkv$60b$1@dont-email.me> <lny34g13xb.fsf@kst-u.example.com> <q8oqkc$ujb$1@dont-email.me>

Show all headers | View raw


Andrey Tarasevich <andreytarasevich@hotmail.com> writes:

> On 4/11/2019 5:08 PM, Keith Thompson wrote:
>
>>> 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?
>
> The end of the "explicit" block and the end of the "implicit"
> block.
>
>> 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.
>
> Exactly.  The word *strict* is used in this text to emphasize the
> fact that these blocks are nested, to emphasize the fact that they
> are different.
>
>> 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.
>
> No, they don't.  For some reason you are attempting to attach
> beginning and termination points of blocks to some physical
> characters in the source code.  The language defines no such
> attachment.  Beginning and termination points are concepts.  And
> two different blocks always have two different termination points.
> Pretty simple.
>
> One might claim that the document is underdefined, but to me it
> looks like a stretch.  The intent is perfectly clear:  introduce
> some extra ("implicit") blocks that envelop the existing blocks.
> These extra blocks are "invisible" but otherwise behave just like
> normal enveloping block do.

I agree with your reading:  the block of a controlling statement
has a different (and larger) scope than those of its controlled
statement(s).  Note further that in C11 a small change in wording
was made regarding scopes.  In C99, 6.2.1 p4 said in part (italics
in original in all cases)

    If so, the scope of one entity (the /inner scope/) will be a
    strict subset of the scope of the other entity (the /outer
    scope/).

In C11, this sentence was changed to say

    If so, the scope of one entity (the /inner scope/) will end
    strictly before the scope of the other entity (the /outer
    scope/).

IMO this change clarifies the "strict subset" wording used
in the text in section 6.8 (Statements).

Incidentally, note that the rules regarding "induced" blocks
and their scopes are slightly different in C than in C++.  For
example, take the following function:

    int
    foo(){
        extern unsigned bas();
        for(  unsigned x = bas();  x;  x /= 2  ){
            int x = 2;
            /* ... potentially more code here ... */
        }
        return  0;
    }

In C, this function definition is legal.  In C++, the 'int x;'
gets flagged as a redeclaration of 'x', even though it is
enclosed in a compound statement.  To me it looks like the C
way is better.  For one thing, this pattern can be used to
guarantee that no modifications happen to the loop control
variable within the loop body.  Also I think the C++ way
runs afoul of the Law of Least Astonishment.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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