Prototype for main()

Discussion in 'C Programming' started by jaysome, Feb 22, 2007.

  1. jaysome

    jaysome Guest

    While looking at the source code for gcc today, I noticed that a
    prototype for main() was declared. From gcc.c:

    extern int main (int, const char **);

    int
    main (int argc, const char **argv)
    {
    ....

    I also noticed that several GNU utilities (e.g., gcov) also declare a
    prototype for main().

    The C standard says that, for main(): "The implementation declares no
    prototype for this function". Did the GNU developers take that to mean
    that the implementation declares no prototype for this function so you
    must declare your own? I always thought that it is superfluous and
    considered bad practice to declare such a prototype (especially in a
    ..c file, as in gcc.c).

    Is there any reason why the gcc source code and other GNU utility
    source code should declare a prototype for main()?

    Regards
    --
    jay
    jaysome, Feb 22, 2007
    #1
    1. Advertising

  2. jaysome

    santosh Guest

    jaysome wrote:
    > While looking at the source code for gcc today, I noticed that a
    > prototype for main() was declared. From gcc.c:
    >
    > extern int main (int, const char **);
    >
    > int
    > main (int argc, const char **argv)
    > {
    > ...
    >
    > I also noticed that several GNU utilities (e.g., gcov) also declare a
    > prototype for main().
    >
    > The C standard says that, for main(): "The implementation declares no
    > prototype for this function". Did the GNU developers take that to mean
    > that the implementation declares no prototype for this function so you
    > must declare your own? I always thought that it is superfluous and
    > considered bad practice to declare such a prototype (especially in a
    > .c file, as in gcc.c).
    >
    > Is there any reason why the gcc source code and other GNU utility
    > source code should declare a prototype for main()?


    Some non-conformant compilers complain when there's no prototype for
    main. I think it's to shut these compiler up, that there's a prototype
    for main. Also if main is called from someother module, a prototype
    enables the compiler to do parameter checking.
    santosh, Feb 22, 2007
    #2
    1. Advertising

  3. jaysome

    Eric Sosman Guest

    jaysome wrote:
    > While looking at the source code for gcc today, I noticed that a
    > prototype for main() was declared. From gcc.c:
    >
    > extern int main (int, const char **);
    >
    > int
    > main (int argc, const char **argv)
    > {
    > ...
    >
    > I also noticed that several GNU utilities (e.g., gcov) also declare a
    > prototype for main().
    >
    > The C standard says that, for main(): "The implementation declares no
    > prototype for this function". Did the GNU developers take that to mean
    > that the implementation declares no prototype for this function so you
    > must declare your own? I always thought that it is superfluous and
    > considered bad practice to declare such a prototype (especially in a
    > .c file, as in gcc.c).
    >
    > Is there any reason why the gcc source code and other GNU utility
    > source code should declare a prototype for main()?


    Unless main() is called recursively (from some module where
    its definition is not visible), I cannot think of a reason to
    write a separate declaration for it.

    Just a guess: Perhaps at some time in the past, the sources
    for these programs were run through a "Create declarations for
    all externally-linked functions" utility. Such a utility might
    not have had a special case to suppress a declaration for main().

    --
    Eric Sosman
    lid
    Eric Sosman, Feb 22, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neelesh

    Prototype of "main"

    Neelesh, Nov 6, 2005, in forum: C++
    Replies:
    9
    Views:
    320
  2. June Lee
    Replies:
    2
    Views:
    799
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    9
    Views:
    186
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    264
  5. javascript fish
    Replies:
    0
    Views:
    168
    javascript fish
    Oct 11, 2008
Loading...

Share This Page