recursive main

This is a discussion on recursive main within the C Programming forums, part of the General Programming Boards category; just wondering if calling main() recursively is a good thing to do? [code] int main() { //something here main(); }...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    recursive main

    just wondering if calling main() recursively is a good thing to do?

    [code]

    int main()
    {
    //something here
    main();
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can not call main recursively per the standard.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    what about C99?

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by paperbox005
    just wondering if calling main() recursively is a good thing to do?

    [code]

    int main()
    {
    //something here
    main();
    }

    You actually can call main recursively. It is rarely done as it it bad practice and who knows what depth of recursion you can call it too. I actually had tested this one time. But it can be done. Implementing it is up to you.

    Thantos may be referring to C++ in that the standard specifically says you cannot call main recusrsively. However in C the standard doesn't explicity state this.
    Last edited by caroundw5h; 08-19-2004 at 09:26 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    19

    stack

    calling main recursively isnt bad but have an eye on the stack space you
    have (it doesnt matter but a big program may cause stack overflow

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Remove all doubt by doing this
    Code:
    int mymain ( int argc, char *argv[] ) {
       // blah
       return mymain ( argc-1, argv+1 );
    }
    
    int main ( int argc, char *argv[] ) {
      return mymain( argc, argv );
    }
    It's just a couple of extra lines

    main() is unique in that it may have a unique calling convention which is incompatible with recursive calls.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >main() is unique in that it may have a unique calling convention which is incompatible with recursive calls.

    Maybe I'm missing what you are saying here? (It wouldn't surprise me.)
    Code:
    #include <stdio.h>
    
    int mymain ( int argc, char *argv[] )
    {
       if ( !argc )
       {
          return 0;
       }
       printf("argc = %d, argv[0] = %s\n", argc, argv[0]);
       return mymain ( argc-1, argv+1 );
    }
    
    int main ( int argc, char *argv[] )
    {
       return mymain( argc, argv );
    }
    
    /* my output
    H:\>test one two three
    argc = 4, argv[0] = H:\test.exe
    argc = 3, argv[0] = one
    argc = 2, argv[0] = two
    argc = 1, argv[0] = three
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I think the point was "Why call main recursively when it takes only a few more lines to call a regular function recursively instead?".

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 04:14 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  3. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. void main
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-29-2002, 07:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21