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


Groups > comp.lang.c > #143017

Re: Scopes of identifiers

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Scopes of identifiers
Date 2019-04-11 10:20 -0700
Organization None to speak of
Message-ID <lnbm1c31di.fsf@kst-u.example.com> (permalink)
References <098da5fe-31d3-47b7-b695-d4faea10b366@googlegroups.com> <e9d0004b-6a21-43ef-98f6-16ed2152d985@googlegroups.com> <bUDrE.35723$lc3.35324@fx02.am4> <MbHrE.95986$D22.88215@fx35.am4>

Show all headers | View raw


Bart <bc@freeuk.com> writes:
> On 11/04/2019 10:36, Bart wrote:
[...]
> Funny that within a short time of posting, somebody posts an example 
> using exactly such a function declaration:
>
> On 11/04/2019 14:07, John Forkosh wrote:
>
>  > /* --- test whether or not two ints are equal,
>  >     without comparisons --- */
>  > #include <stdio.h>
>  > #include <stdlib.h>
>  > int main ( int argc, char *argv[] ) {
>  >    int  i = (argc>1? atoi(argv[1]) : 1 ),
>  >         j = (argc>2? atoi(argv[2]) : 2 );
>  >    int  isequal();
>  >    fprintf( stdout," %d %s %d? \n",
>  >         i, (isequal(i,j)?"==":"=/="), j);
>  >    } /* --- end-of-function main() --- */
>  >
>  > int isequal ( int i, int j ) {
>  >    return ( !( abs(i-j) ) );
>  >    } /* --- end-of-function isequal() --- */
>
> The isequal() declaration is inside main().
>
> I've tweaked my compiler to accept this (basically, added a couple of 
> lines to read the declaration and restrict the error to function 
> definitions), although the function name still has module-wide scope. 
> (Which it has to have, but you might want, for some reason, to restrict 
> its visibility.)

By "module-wide scope", I'm guessing you mean file scope.

The declaration inside main gives the identifier "isequal" block
scope.  The declaration that's part of the definition gives it
file scope, but makes it visible only from the declaration to the
end of the file.  The name is not visible inside main prior to the
declaration, or outside any function prior to the definition.

(Incidentally, it would be better for the declaration to be a
prototype so the compiler can check that it has the right type.)

Personally, I would either have moved the definition of isequal
above the definition of main, making the extra declaration
unnecessary, or put the "forward" declaration at the top level,
before main().  But declaring it inside a block in main(), where
it's used, is not unreasonable.

-- 
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


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