What will happen if main called in side main function?

Discussion in 'C Programming' started by Ravi, Mar 31, 2006.

  1. Ravi

    Ravi Guest

    void main()
    {
    main();
    }

    int main()
    {
    main();
    }
    Ravi, Mar 31, 2006
    #1
    1. Advertising

  2. Ravi

    Eric Sosman Guest

    Ravi wrote On 03/31/06 12:03,:
    > void main()
    > {
    > main();
    > }


    This example is incorrect because main() returns an
    `int'; it is not a `void' function. The C language does
    not define what will happen when you mis-define main()
    this way; however, on many implementations the effect
    will be similar to the second example.

    > int main()
    > {
    > main();
    > }


    This example will execute forever, provided the machine
    running it has infinite resources. Less powerful machines
    may be unable to run this program to completion ...

    --
    Eric Sosman, Mar 31, 2006
    #2
    1. Advertising

  3. Ravi

    Ravi Guest

    But i have read that C can have only one main function, so is this
    valid?
    Ravi, Mar 31, 2006
    #3
  4. Ravi

    Jordan Abel Guest

    On 2006-03-31, Ravi <> wrote:
    > But i have read that C can have only one main function, so is this
    > valid?


    But a function can call itself.
    Jordan Abel, Mar 31, 2006
    #4
  5. "Ravi" <> wrote in message
    news:...
    > But i have read that C can have only one main function, so is this
    > valid?
    >

    Please always include the context of your message.

    int main() {
    main();
    return 0;
    }

    There is only one instance of main() defined. It just happens to recursively
    call itself.
    Forever.

    This, on the other hand, will eventually stop:

    int num = 10;
    int main() {
    if ( --num > 0 ) {
    main();
    }
    return 0;
    }
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
    Fred Kleinschmidt, Mar 31, 2006
    #5
  6. Ravi

    Flash Gordon Guest

    Eric Sosman wrote:
    >
    > Ravi wrote On 03/31/06 12:03,:


    <snip>

    >> int main()
    >> {
    >> main();
    >> }

    >
    > This example will execute forever, provided the machine
    > running it has infinite resources. Less powerful machines
    > may be unable to run this program to completion ...


    A slightly better implementation without infinite resources might use
    tail recursion optimisation and convert it in to a simple loop that only
    terminates when the machine itself is terminated.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
    Flash Gordon, Mar 31, 2006
    #6
  7. Ravi

    Jack Klein Guest

    On 31 Mar 2006 09:03:16 -0800, "Ravi" <> wrote
    in comp.lang.c:

    > void main()
    > {
    > main();
    > }
    >
    > int main()
    > {
    > main();
    > }


    It seems apparent from a later message you posted in the thread, you
    are asking if the above is a valid program. No, it is not, you cannot
    have two functions with external linkage and the same name in a
    program. And you cannot have two functions with the same name in a
    single translation unit, regardless of linkage.

    And main() must be defined with a return type of int.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Mar 31, 2006
    #7
  8. Ravi

    Guest


    > > int main()
    > > {
    > > main();
    > > }

    >
    > This example will execute forever, provided the machine
    > running it has infinite resources. Less powerful machines
    > may be unable to run this program to completion ...


    I don't beleive any machine can run this to completion since the
    program operates recursively forever. Also this should cause a compiler
    warning about no return statement in a non void function, at least on
    any compiler I have ever used.
    , Mar 31, 2006
    #8
  9. Ravi

    Skarmander Guest

    wrote:
    >>> int main()
    >>> {
    >>> main();
    >>> }

    >> This example will execute forever, provided the machine
    >> running it has infinite resources. Less powerful machines
    >> may be unable to run this program to completion ...

    >
    > I don't beleive any machine can run this to completion since the
    > program operates recursively forever.


    I do believe the person you failed to attribute was trying to make a joke.
    The standard form would be "the new X is so fast, it can execute an infinite
    loop in under Y seconds".

    > Also this should cause a compiler warning about no return statement in a
    > non void function, at least on any compiler I have ever used.
    >

    GCC with -std=c99 will not issue a warning, because in C99 it's legal to
    omit the return statement from main() (and *only* main()), in which case a
    return value of 0 is implied.

    I don't think I've yet read why this was considered a good idea; maybe to
    retroactively fix all the broken code that invoked undefined behavior.

    S.
    Skarmander, Mar 31, 2006
    #9
  10. Skarmander <> writes:
    [...]
    > GCC with -std=c99 will not issue a warning, because in C99 it's legal
    > to omit the return statement from main() (and *only* main()), in which
    > case a return value of 0 is implied.
    >
    > I don't think I've yet read why this was considered a good idea; maybe
    > to retroactively fix all the broken code that invoked undefined
    > behavior.


    Omitting the return statement in main() never invoked undefined
    behavior (unless a recursive call to main() attempts to use the
    result). At worst, it merely returns an unspecified status to the
    calling environment, and the behavior of the calling environment is
    outside the scope of the C standard.

    I think part of the motivation for the change is that made some of the
    examples in K&R retroactively valid.

    The Rationale (C99RationaleV5.10.pdf) doesn't mention this change as
    far as I can tell.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Mar 31, 2006
    #10
  11. Ravi

    Skarmander Guest

    Keith Thompson wrote:
    > Skarmander <> writes:
    > [...]
    >> GCC with -std=c99 will not issue a warning, because in C99 it's legal
    >> to omit the return statement from main() (and *only* main()), in which
    >> case a return value of 0 is implied.
    >>
    >> I don't think I've yet read why this was considered a good idea; maybe
    >> to retroactively fix all the broken code that invoked undefined
    >> behavior.

    >
    > Omitting the return statement in main() never invoked undefined
    > behavior (unless a recursive call to main() attempts to use the
    > result). At worst, it merely returns an unspecified status to the
    > calling environment, and the behavior of the calling environment is
    > outside the scope of the C standard.
    >

    Yes, true. Although some calling environments will get quite upset if you
    don't return a decent value from main(), the hissy fit they throw is not
    undefined behavior.

    I'm confusing this with declaring main as returning void, which is a lot worse.

    > I think part of the motivation for the change is that made some of the
    > examples in K&R retroactively valid.
    >

    That's... interesting. I suppose it really doesn't matter much, as it's a
    minor thing. But still, a peculiar addition.

    S.
    Skarmander, Mar 31, 2006
    #11
  12. In article <>,
    <> wrote:

    >> > int main()
    >> > {
    >> > main();
    >> > }


    >Also this should cause a compiler
    >warning about no return statement in a non void function, at least on
    >any compiler I have ever used.


    Since the recursive call to main() never returns, the end of the
    function cannot be reached, so a return statement would be
    superfluous. Indeed, a sufficiently good compiler would warn if
    you did have one!

    -- Richard
    Richard Tobin, Mar 31, 2006
    #12
  13. (Richard Tobin) writes:
    > In article <>,
    > <> wrote:
    >
    >>> > int main()
    >>> > {
    >>> > main();
    >>> > }

    >
    >>Also this should cause a compiler
    >>warning about no return statement in a non void function, at least on
    >>any compiler I have ever used.

    >
    > Since the recursive call to main() never returns, the end of the
    > function cannot be reached, so a return statement would be
    > superfluous. Indeed, a sufficiently good compiler would warn if
    > you did have one!


    A sufficiently good compiler would translate the program to a single
    instruction that does the equivalent of a stack overflow.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 1, 2006
    #13
  14. Ravi

    Skarmander Guest

    Keith Thompson wrote:
    > (Richard Tobin) writes:
    >> In article <>,
    >> <> wrote:
    >>
    >>>>> int main()
    >>>>> {
    >>>>> main();
    >>>>> }
    >>> Also this should cause a compiler
    >>> warning about no return statement in a non void function, at least on
    >>> any compiler I have ever used.

    >> Since the recursive call to main() never returns, the end of the
    >> function cannot be reached, so a return statement would be
    >> superfluous. Indeed, a sufficiently good compiler would warn if
    >> you did have one!

    >
    > A sufficiently good compiler would translate the program to a single
    > instruction that does the equivalent of a stack overflow.
    >

    Actually, I would be impressed if the compiler turned it into a self-jump
    instead. Optimizing tail recursion is not very hard, but it generally
    doesn't pay off in imperative languages, so you don't see many C compilers
    doing it.

    Of course, an even more efficient compiler would use the equivalent of HLT,
    to save processor cycles.

    S.
    Skarmander, Apr 1, 2006
    #14
  15. Ravi

    Jordan Abel Guest

    On 2006-03-31, Skarmander <> wrote:
    > Keith Thompson wrote:
    >> (Richard Tobin) writes:
    >>> In article <>,
    >>> <> wrote:
    >>>
    >>>>>> int main()
    >>>>>> {
    >>>>>> main();
    >>>>>> }
    >>>> Also this should cause a compiler
    >>>> warning about no return statement in a non void function, at least on
    >>>> any compiler I have ever used.
    >>> Since the recursive call to main() never returns, the end of the
    >>> function cannot be reached, so a return statement would be
    >>> superfluous. Indeed, a sufficiently good compiler would warn if
    >>> you did have one!

    >>
    >> A sufficiently good compiler would translate the program to a single
    >> instruction that does the equivalent of a stack overflow.
    >>

    > Actually, I would be impressed if the compiler turned it into a self-jump
    > instead. Optimizing tail recursion is not very hard, but it generally
    > doesn't pay off in imperative languages, so you don't see many C compilers
    > doing it.
    >
    > Of course, an even more efficient compiler would use the equivalent of
    > HLT, to save processor cycles.


    Or just have it exit after a set time. Would be really impressive on
    benchmarks.
    Jordan Abel, Apr 1, 2006
    #15
  16. Ravi

    Guest

    Richard Tobin wrote:
    > In article <>,
    > <> wrote:
    >
    > >> > int main()
    > >> > {
    > >> > main();
    > >> > }

    >
    > >Also this should cause a compiler
    > >warning about no return statement in a non void function, at least on
    > >any compiler I have ever used.

    >
    > Since the recursive call to main() never returns, the end of the
    > function cannot be reached, so a return statement would be
    > superfluous. Indeed, a sufficiently good compiler would warn if
    > you did have one!
    >
    > -- Richard


    I can't get a warning out of my compilers for the following:

    int main()
    {
    main();
    return 0;
    }

    However I do get warning for the original one with no return statement.
    I would think that in general irt would be very hard for the compiler
    to recognize that recursion is going to continue forever.
    , Apr 1, 2006
    #16
  17. Ravi

    Skarmander Guest

    wrote:
    > Richard Tobin wrote:
    >> In article <>,
    >> <> wrote:
    >>
    >>>>> int main()
    >>>>> {
    >>>>> main();
    >>>>> }
    >>> Also this should cause a compiler
    >>> warning about no return statement in a non void function, at least on
    >>> any compiler I have ever used.

    >> Since the recursive call to main() never returns, the end of the
    >> function cannot be reached, so a return statement would be
    >> superfluous. Indeed, a sufficiently good compiler would warn if
    >> you did have one!
    >>
    >> -- Richard

    >
    > I can't get a warning out of my compilers for the following:
    >
    > int main()
    > {
    > main();
    > return 0;
    > }
    >
    > However I do get warning for the original one with no return statement.
    > I would think that in general irt would be very hard for the compiler
    > to recognize that recursion is going to continue forever.
    >

    Actually, this isn't just very hard, it's impossible in general.
    http://en.wikipedia.org/wiki/Halting_problem

    S.
    Skarmander, Apr 1, 2006
    #17
  18. Ravi wrote:
    >
    > But i have read that C can have only one main function, so is this
    > valid?


    Please see the other posts about how to properly reply using Google's
    interface.

    Here is the code from your original post:

    > void main()
    > {
    > main();
    > }
    >
    > int main()
    > {
    > main();
    > }


    If you mean to have both of these functions in one file, then this is
    not valid. However, this would be true regardless of the name of the
    function.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Apr 1, 2006
    #18
    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. aa
    Replies:
    1
    Views:
    336
    Joaquin Corchero
    Mar 8, 2005
  2. Xiangliang Meng
    Replies:
    2
    Views:
    410
    Jack Klein
    Jun 21, 2004
  3. main() called inside main()

    , May 4, 2006, in forum: C Programming
    Replies:
    14
    Views:
    1,426
    Richard Heathfield
    May 7, 2006
  4. ravi
    Replies:
    28
    Views:
    979
    Richard Heathfield
    Sep 26, 2007
  5. U.Mutlu
    Replies:
    14
    Views:
    427
    Gernot Frisch
    Feb 7, 2012
Loading...

Share This Page