Re: Ahead of "main"?



On Sun, 29 Apr 2007 21:40:00 +0100, "Malcolm McLean"
<regniztar@xxxxxxxxxxxxxx> wrote:


"mdh" <mdeh@xxxxxxxxxxx> wrote in message
news:1177877117.874830.65670@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi all,
Going quite methodically through K& R ( as some of you can attest
to!), I have never seen a big diffference in declaring a function
within "main" or "ahead" of it. Now, (p119, K&R II), the discussion
states that "functions "whatever" " should be declared ahead of
main.
Is there a good reason for this?
thanks.

Some early versions of C had local functions, declared within the function
that called them. The idea never caught on, and it is now not possible to
declare functions within main.

It is certainly possible to declare a function within another.
Defining is prohibited but declaring is allowed. The drawback is that
the declaration has block scope and will not be visible to any other
function in the translation unit.

Old C also had no prototypes. So if you put functions in reverse order of

Not always possible if function1 and function2 call each other as part
of a recursive algorithm.

hierarchy, the compiler could do additional checking of arguments. Nowadays
we should prototype all functions, so it doesn't matter where main() is
placed, though obviously it should be either the first or the last function
for readbility.


Remove del for email
.



Relevant Pages

  • Ahead of "main"?
    ... Going quite methodically through K& R (as some of you can attest ... I have never seen a big diffference in declaring a function ... Is there a good reason for this? ...
    (comp.lang.c)
  • Re: Ahead of "main"?
    ... Going quite methodically through K& R (as some of you can attest ... I have never seen a big diffference in declaring a function ... Old C also had no prototypes. ...
    (comp.lang.c)
  • Re: Ahead of "main"?
    ... Barry Schwarz wrote, On 29/04/07 23:29: ... Going quite methodically through K& R (as some of you can attest ... Defining is prohibited but declaring is allowed. ... concerned, since on page 3 it says, "Function definitions cannot be nested..." ...
    (comp.lang.c)
  • Re: Ahead of "main"?
    ... Going quite methodically through K& R (as some of you can attest ... I have never seen a big diffference in declaring a function ... In Standard C, functions cannot be declared "within" main; ...
    (comp.lang.c)
  • Re: [PATCH 4/4] [RFC] UIO: generic platform driver
    ... The macro definition and the ... BUG_ON for "declaring" preconditions. ... That's a matter of taste, too, I like it that way. ... matter for the compiler, but this way it's one goto ...
    (Linux-Kernel)