Re: Ahead of "main"?



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

In Standard C, functions cannot be declared "within" main(); these are called nested functions and are implemented as an extension by some compilers, but in general you should never use them. Therefore, the debate is about whether to declare functions before or after main().

As a rule, you should declare all functions before you call them; I won't go into the reasons why, as Eric did a good job of that. One style is to define all other functions before main(), which also declares them. The other is to declare all of your functions in a group near the beginning of the source, and then you can define them in any order you want. The latter style is effectively required when you move to multi-file projects, and the standard practice is to put all of your function declarations in header (.h) files so that each source file can simply #include the appropriate header files and then use whatever functions are needed.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov


--
Posted via a free Usenet account from http://www.teranews.com

.



Relevant Pages

  • Re: question-- a little confused
    ... > 3) You can declare any number of non public classes in the same file. ... same source file but you need to make sure that only one of them is defined ... if you decide to define class Money as public and remove the ... If you decide to define class Driver as public and remove the ...
    (comp.lang.java.programmer)
  • Re: Use of static ?
    ... >> It isn't in global space in that example: ... That is not the main reason to declare it static. ... If it is not declared static and you have in an unrelated source file ...
    (comp.lang.c)
  • Re: Ahead of "main"?
    ... The other is to declare all of your functions in a group near the beginning of the source, and then you can define them in any order you want. ... The latter style is effectively required when you move to multi-file projects, and the standard practice is to put all of your function declarations in header files so that each source file can simply #include the appropriate header files and then use whatever functions are needed. ...
    (comp.lang.c)
  • Re: A problem in compiling sqlrpg source
    ... It looks as if you have a space in the wrong place in the "DECLARE C1 CURSOR ... This might make the compiler think that you were trying to open the cursor ... source file is "sqlrpg". ... C/EXEC SQL WHENEVER NOT FOUND GO TO DONE1 ...
    (comp.sys.ibm.as400.misc)
  • Re: Ahead of "main"?
    ... In Standard C, functions cannot be declared "within" main; ... you should declare all functions before you call them; ... source file and declared static in that source file. ... You should not put any function definitions in a header file. ...
    (comp.lang.c)