Recursing main

This is a discussion on Recursing main within the C++ Programming forums, part of the General Programming Boards category; Can you recurse main? ie [code] int main(void) { int x = 0; cout << x++ << endl; while(x<40) main(); ...

  1. #1
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942

    Recursing main

    Can you recurse main?
    ie

    [code]
    int main(void)
    {
    int x = 0;
    cout << x++ << endl;
    while(x<40)
    main();
    }

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Yes, but it is not advised! Some compilers will give you an error/warning.

    --edit-- Like salem said though, it will be an infinite loop, not recursion that has a basecase to stop the recursion.
    Last edited by stumon; 06-30-2003 at 05:09 PM.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> If you really must do it

    Though I really see no reason why you ever would need to.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    I know. I was just wondering

  5. #5
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    >>Yes, but it is not advised!
    No, C++ doesn't allow recursive calls to main...ever. You can try to compile as C, then you can recursively call main, but that's not the same question.

    >>Some compilers will give you an error
    Yes, that would be C++ compilers.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> Yes, that would be C++ compilers.

    Only completely standard compliant compilers. I have seen C++ compilers allow people to call main recursively with little more than a small warning to the effect of, "You shouldn't do this."
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Originally posted by Casey
    >>Yes, but it is not advised!
    No, C++ doesn't allow recursive calls to main...ever. You can try to compile as C, then you can recursively call main, but that's not the same question.

    >>Some compilers will give you an error
    Yes, that would be C++ compilers.
    Im not going to argue, im only going to post this once. Put this in your compiler.
    Code:
    //filename blah1.cpp
    #include <iostream>
    
    int main()
    {
    	
    	std::cout << "Blah";
    	main();
    	return 0;
    }
    And it will work depending on your compiler! infinite loop! Not exactly recursion, but similar. That was my answer! VSC++ will allow this with no errors/warnings. Some others will not.
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confused about adding controls to main window
    By terracota in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2004, 12:35 PM
  2. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM
  3. Int Main or Void Main?
    By FromHolland in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2003, 05:29 PM
  4. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 10:46 PM
  5. getting a function to return to main??
    By Neildadon in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2002, 10:24 AM

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