main prototype pointless?

Discussion in 'C Programming' started by Zach, Feb 2, 2009.

  1. Zach

    Zach Guest

    I was just reading "Advanced C" and one of the example programs I
    looked at had main prototyped:

    int main void();

    int main()
    {
    ....
    }

    Is there *any* reason to do this or is it just a waste of time?

    What do the C standards have to say about it?

    Can anyone think of a case when you would want to prototype main()?

    Zach
     
    Zach, Feb 2, 2009
    #1
    1. Advertising

  2. Zach

    Guest

    On 2 Feb, 09:36, Zach <> wrote:

    > I was just reading "Advanced C" and one of the example programs I
    > looked at had main prototyped:
    >
    > int main void();


    I really hope it didn't say that...


    > int main()
    > {
    > ...
    >
    > }
    >
    > Is there *any* reason to do this or is it just a waste of time?


    it's a waste of time. What they should do is this

    int main (void)
    {
    }

    as that is both a definition and a protoype

    > What do the C standards have to say about it?


    roughly what I said but in a far more elegant and humourous way

    > Can anyone think of a case when you would want to prototype main()?

    maybe if you had some bizzare recursive or mutual recursive
    call of main you might need a separate prototype

    int main (void);

    int main_caller (int n)
    {
    if (n > 0)
    main();
    }

    int main(void)
    {
    static int i = 2;
    i--;
    main_caller (i);
    return 0;
    }

    but that sort of stuff is only fit for the OCC


    --
    Nick Keighley

    "It is a good rule in life never to apologize. The right sort of
    people do not want apologies, and the wrong sort take a mean
    advantage from them." - P. G. Wodehouse
     
    , Feb 2, 2009
    #2
    1. Advertising

  3. Zach

    jameskuyper Guest

    Zach wrote:
    > I was just reading "Advanced C" and one of the example programs I
    > looked at had main prototyped:
    >
    > int main void();


    As written, this is a non-prototype declaration that is permitted only
    if both 'main' and 'void' are macros #defined to be something quite
    different from what they appear to be. I presume you intended

    int main(void);

    > int main()
    > {
    > ...
    > }
    >
    > Is there *any* reason to do this or is it just a waste of time?
    >
    > What do the C standards have to say about it?


    I have vague memories that prior declarations main() are prohibited,
    but I couldn't find any such prohibition. It's therefore a perfectly
    ordinary declaration, and therefore allowed. Whether or not it's a
    good idea is not the kind fo thing the standard normally discusses.

    > Can anyone think of a case when you would want to prototype main()?


    I think it's a good idea to prototype all functions, even main(), if
    only for the same of consistency. However, I think you're actually
    asking a slightly different question. I think you're asking about
    providing a seperate prototype declaration for main() prior to the one
    that should start the definiton of main.

    If your program calls main() from other translation units, it should
    pre-declare it in a seperate header, which is #defined in all
    translation units where main() is either defined or used. If you call
    it only in the same translation unit, but prior to the definition of
    main(), you must also pre-declare main(). However, in such a case, I
    would prefer to avoid the need for the seperate declaration by making
    the defintion of main() itselt a prototype, prior to the first call to
    main(). In all other cases, a prior declaration is pointless.

    However, I doubt that this is the reason for the code you've seen. I
    suspect that it's meant to shut up a warning from a compiler.

    Many good compilers will give you a warning about defining a non-
    static function without already having a previous declaration in
    scope. This is a good thing, in general; if a function is defined or
    used in two or more tranlsation units, a declaration of that function
    should be put into a header file and shared between all of those
    translation units using #Include. This will give you a warning message
    in the event that there's any discrepancy between the declaration and
    either the definition of the function or the uses of the function.

    If a function will only be used in one translation unit, it should be
    declared static. In some cases this will enable some better
    optimiizations; in fact, it's been argued that 'inline' is redundant,
    because static is sufficient to enable the same optimizations allowed
    by 'inline', in almost every case where such optimizations are
    desireable. However, static also enables the compiler to warn you if
    you make no use of the function anywhere in the same translation unit
    where it was defined; for static functions, any such function
    definition is pointless, and probably an error.

    main() is an exception to the above rules. However, I believe that
    some of the compilers that provide this warning don't make an
    exception for main().
     
    jameskuyper, Feb 2, 2009
    #3
  4. Zach

    Nelu Guest

    On Mon, 02 Feb 2009 01:36:03 -0800, Zach wrote:

    > I was just reading "Advanced C" and one of the example programs I looked
    > at had main prototyped:
    >
    > int main void();


    This is not right. It should be: int main(void);

    >
    > int main()
    > {
    > ...
    > }
    >
    > Is there *any* reason to do this or is it just a waste of time?


    I'm not sure there's any reason to do that. Just write:
    int main(void) {
    ...
    }
    which both declares and defines the function.

    >
    > What do the C standards have to say about it?


    I'm not sure but I don't think they disallow it.

    >
    > Can anyone think of a case when you would want to prototype main()?


    You may want to do that if you prototype all your functions, as a habit.
    But, given the fact that main is the entry point in your application and
    it's not likely that you would call it like you would call any other
    function, I doubt there's a good reason to do that.



    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org
     
    Nelu, Feb 2, 2009
    #4
  5. On Mon, 2 Feb 2009 01:36:03 -0800 (PST), Zach <> wrote:
    >What do the C standards have to say about it?


    ISO/IEC 9899:1999 (E) ©ISO/IEC
    5.1.2.2.1 Program startup
    ....It shall be defined...

    int main(void) { /* ... */ }
    int main(int argc, char *argv[]) { /* ... */ }


    >Can anyone think of a case when you would want to prototype main()?


    ibid. The implementation declares no prototype for this function.
     
    Kevin D. Quitt, Feb 2, 2009
    #5
  6. Zach

    jameskuyper Guest

    Kevin D. Quitt wrote:
    > On Mon, 2 Feb 2009 01:36:03 -0800 (PST), Zach <> wrote:
    > >What do the C standards have to say about it?

    >
    > ISO/IEC 9899:1999 (E) �ISO/IEC
    > 5.1.2.2.1 Program startup
    > ...It shall be defined...
    >
    > int main(void) { /* ... */ }
    > int main(int argc, char *argv[]) { /* ... */ }
    >
    >
    > >Can anyone think of a case when you would want to prototype main()?

    >
    > ibid. The implementation declares no prototype for this function.


    The question was a prototype declaration provided by the user code,
    not by the implementation.
     
    jameskuyper, Feb 2, 2009
    #6
    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. jsnX

    pointless object copies

    jsnX, Mar 31, 2005, in forum: C++
    Replies:
    1
    Views:
    423
    Victor Bazarov
    Mar 31, 2005
  2. mk
    Replies:
    2
    Views:
    294
    Terry Reedy
    Nov 24, 2009
  3. Antoine Pitrou

    Re: pointless musings on performance

    Antoine Pitrou, Nov 24, 2009, in forum: Python
    Replies:
    9
    Views:
    349
    Paul Boddie
    Nov 26, 2009
  4. John Carter
    Replies:
    3
    Views:
    129
    Brian Candler
    Jan 16, 2009
  5. Intransition

    FileUtils.mv Pointless Errno::ENOENT

    Intransition, Sep 6, 2009, in forum: Ruby
    Replies:
    2
    Views:
    174
    7stud --
    Sep 12, 2009
Loading...

Share This Page