-2

I am relearning C++. I was quite a noob before but I am going over the basics again and hopefully going further. My main question is, at the end of the main loop, is there a way to call the function again instead of ending the programme by returning 0. So something along the line of:

    ........
    return main;
}
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
vulcanicrook
  • 53
  • 1
  • 1
  • 5
  • 3
    No, you can't do that. Maybe look up "loops". – Galik Sep 12 '16 at 01:05
  • 1
    Yes, just call the function recursively. – sashang Sep 12 '16 at 01:06
  • 1
    @Galik: I don't see why you couldn't. I mean, you'd have to actually call the `main` function, and you'd eventually run out of stack space if you recursed indefinitely, and loops are a better idea, but you _could_. – ShadowRanger Sep 12 '16 at 01:07
  • 1
    @sashang: http://stackoverflow.com/questions/4238179/calling-main-in-main-in-c – Andrew Li Sep 12 '16 at 01:08
  • 3
    @ShadowRanger Its undefined behavior. The `C++ Standard` says you can not do it. – Galik Sep 12 '16 at 01:08
  • 1
    Calling main() yourself is undefined behavior ( http://stackoverflow.com/questions/4518598/is-it-legal-to-recurse-into-main-in-c ) ... but of course you could create your own function and have main() call it, and then it could call itself... – Jeremy Friesner Sep 12 '16 at 01:08
  • @Galik: Gotcha. Learn something new every day. – ShadowRanger Sep 12 '16 at 01:10
  • @AndrewL. Oh it's legal in C but not C++...interesting. – sashang Sep 12 '16 at 01:12
  • @Galik I forgot to say without creating a loop, because i found some old codes where it's saved as return main;. But when i compile it gives error. Furthermore I have an executable with same name as cpp so it got me confused. – vulcanicrook Sep 12 '16 at 02:22

2 Answers2

5

The standard C++ says...

5.2.2.9 Recursive calls are permitted, except to the function named main

"The Standard" at https://isocpp.org/std/the-standard

Loreto
  • 674
  • 6
  • 20
  • 1
    Might i suggest adding a link to where you found this? assuming that this is on the internet. – Christian Dean Sep 12 '16 at 01:17
  • 1
    @Loreto no, no. I meant just add the link in your answer, just incase anyone might be interested in seeing where the standard says this. You didn't have to show me. I already believe you ;) – Christian Dean Sep 12 '16 at 01:23
  • Thank you @Loreto that was very clear, i struggled to find a clear answer beofre. – vulcanicrook Sep 12 '16 at 02:24
0

You could certainly do something like this:

int main()
{
    while(1==1) /* (1==1) is always true. */
    {
        /* Do the hokey pokey, turn yourself around, and do whatever else you feel like doing. */
    }
    /* The program will never reach this point unless you explicitly break out of the loop. */
    return 0;
}

This will result in a program that just repeats over and over again until you kill it. This is, of course, bad style, but it will work if you just need to get something up and running quickly.

As Trevor Hickey mentions, you could call break from within the loop and break out.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40