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


Groups > comp.lang.c > #41377

Re: complex declarations

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: complex declarations
Date 2014-03-05 08:13 -0500
Organization A noiseless patient Spider
Message-ID <lf77ub$iu8$1@dont-email.me> (permalink)
References <lf5cdv$dll$1@dont-email.me> <53163C3C.8060203@verizon.net> <5316406C.9050905@verizon.net> <lf74lv$s1q$1@dont-email.me>

Show all headers | View raw


On 03/05/2014 07:18 AM, Robbie Brown wrote:
> On 04/03/14 21:06, James Kuyper wrote:
>> On 03/04/2014 03:49 PM, James Kuyper wrote:
>> ,,,
>>> C function definitions can only occur at file scope (6.9p1). You can
>>> declare a function at block scope (6.8p2), but you can't define it
>>> there. Because you can't define it with block scope, I also personally
>>> feel it's a bad idea to declare it at file scope - but that's just a
>>
>> "file" should have been "block" in that last sentence. That error almost
>> exactly reverses my intended meaning.
> 
> OK, I suppose I'm pushing my luck here but I'm not entirely sure what 
> you are saying so here goes
> 
> If I have a 'program' foo.c that (tries to) comply with some seemingly 
> nebulous set of rules that are apparently defined in the latest spec I 
> can find 'for free' (n1256.pdf) then I can't have nested functions for 
> one thing. So, where exactly should I define my functions?

At file scope, in as many or as few different files as you like.

> If I have an 'all in one file program' then I don't need function 
> prototypes and I declare and define all my sub-functions, that is 
> functions that are not main() at the top of the file, once, and use them 
> in main(). I'd call this 'file scope'.
> 
> I can also declare prototypes at the top of the file and define the 
> functions after the end of main() although why I'd want to do this I 
> have no idea. Also 'file scope'

Some people (not including myself) prefer that approach because they
consider it more logical.

> I can also declare prototypes at the head of main or in an include file 
> and define them somewhere else. What scope is this?

That depends upon what you mean by "head of main", and where you
#include the header file:

bar.h:
#ifndef H_BAR
#define H_BAR
int bar(int);
#endif

foo.h:
#ifndef H_FOO
#define H_FOO
void foo();
#endif

main.c:
// Is this what you mean by "head of main"?
// create a file scope declaration for bar():
#include "bar.h"
int file_scope_declaration(double);

int main()
{
    // Or is this what you mean by "head of main"?
    // NB: I do NOT recommend block scope function declarations
    double block_scope_declaration(int);
    // Create block scope declaration for foo()
    #include "foo.h"
    // Do NOT do this with any standard header - the behavior is
    // undefined (7.1.2p4).

    foo();

    return bar(file_scope_declaration(block_scope_declaration(0));
}


> There are other questions, like, where's the sanity of declaring a 
> prototype in block scope when you can't define it in same (this seems 
> particularly bizarre)

While I agree with you about that, those who disagree with us say that
by doing so, they're limiting the scope of the prototype, reducing the
danger of naming conflicts.
-- 
James Kuyper

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


Thread

complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-04 20:18 +0000
  Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-04 15:49 -0500
    Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-04 16:06 -0500
      Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-05 12:18 +0000
        Re: complex declarations "BartC" <bc@freeuk.com> - 2014-03-05 12:56 +0000
          Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-05 08:24 -0500
            Re: complex declarations "BartC" <bc@freeuk.com> - 2014-03-05 16:26 +0000
        Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-05 08:13 -0500
          Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-05 13:32 +0000
        Re: complex declarations Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-05 14:32 +0000
    Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-04 21:42 +0000
      Re: complex declarations Keith Thompson <kst-u@mib.org> - 2014-03-04 14:04 -0800
        Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-05 09:22 +0000
          Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-05 07:29 -0500
            Re: complex declarations Keith Thompson <kst-u@mib.org> - 2014-03-05 08:38 -0800
      Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-04 17:05 -0500
  Re: complex declarations Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-04 21:15 +0000
    Re: complex declarations "BartC" <bc@freeuk.com> - 2014-03-04 21:38 +0000
      Re: complex declarations Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-04 23:27 +0000
    Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-04 21:59 +0000
      Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-04 17:12 -0500
      Re: complex declarations Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-05 00:28 +0000
        Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-05 09:10 +0000
          Re: complex declarations Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-05 11:44 +0000

csiph-web