Thread: Calling main() from within it... is it possible?

  1. #1
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46

    Talking Calling main() from within it... is it possible?

    Is it possible to call the main() function from within it?

    ---or---

    to call it from another function defined outside of it?

    <---- Pritin

  2. #2
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    nope, (correct me if I'm wrong anyone) I dont think you can just "call" the main function. but you can add parameters and pass it parameters when you execute the program or run it from a script.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In the 1989 C standard, calling main() is allowed. In C++, it is explicitly forbidden. I'm not sure offhand about what the 1999 C standard says, but suspect it would allow calling main().

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    int MyMain()
    {
    	if(whatever)
    	{
    		MyMain();
    	}
    
    }
    
    
    int main()
    {
    
    	MyMain();
    }

  5. #5
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46
    The code by "Fordy" I can understand, .. i know that just any function can be called from within it, but what about main()?

    I thought of one method, that is by putting the whole of main() in a do while sequence, and if required to repeat, main() can be somewhat"called" again... That's one way i need ed to use it...

    But what if I want to to call main() freom another function??

    The exact way i want to implement it, is for a very simple that can perform calculations on the eqns of motion... There is an error() function to display errors, but i want it to redirect to main() after displaying errors, which i want to do.... fxns are defined before main()

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    If calling main is illegal in the version/fork of C you're using, then use fordy's example -- just use main as a wrapper of your "real" main function.

  7. #7
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46
    Oh... now i get it... Great. Thanks everyone!! <A HAPPY FACE>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. " void main() is evil. Use int main(). "
    By Brw_Abhi in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2007, 12:16 PM
  2. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  3. Calling a function...
    By Taka in forum C Programming
    Replies: 6
    Last Post: 10-02-2006, 01:04 AM
  4. Main Declaration error
    By starkhorn in forum C++ Programming
    Replies: 11
    Last Post: 06-22-2005, 09:04 AM
  5. void main
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-29-2002, 07:08 PM