Re: Ahead of "main"?



Barry Schwarz wrote, On 29/04/07 23:29:
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.

Of course, if Malcolm meant define, he was also wrong as far as K&R1 is concerned, since on page 3 it says, "Function definitions cannot be nested..."

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.

Pre-ANSI there was no requirement for the compiler to check the parameters even if the function was defined before use. Also on page 3 of K&R1 it refers to using lint to detect inconsistent argument usage.
--
Flash Gordon
Now with a near-mint condition copy of K&R1 courtesy of my company having a clear-out.
.



Relevant Pages

  • 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"?
    ... Going quite methodically through K& R (as some of you can attest ... I have never seen a big diffference in declaring a function ... Defining is prohibited but declaring is allowed. ... so it doesn't matter where mainis ...
    (comp.lang.c)
  • 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 ... In Standard C, functions cannot be declared "within" main; ...
    (comp.lang.c)