Can main() be called recursively?

Can main() be called recursively?
yes but I have never seen a good reason to do so.
The global function main() cannot be called recursively.

For other restrictions and special properties, see: http://en.cppreference.com/w/cpp/language/main_function
Just to add to what has already been said, generally none of the entry points to a binary should be called from within. Even if your compiler allows it, and some of them do I've noticed, it raises holy heck with program flow.
Although not possible in c++, but in c main can be called recursively:

1
2
3
4
5
6
7
8
#include <stdio.h>

main(int argc, char **argv) {
    if (argc > 1) {
        printf("%s\n", argv[--argc]);
        main(argc, argv);
    }
}


Works even with the latest version of c (C11)
i dont think there is ever a good reason to call main. you can use loops and gotos (although i wouldnt. bad for flow. some good cases though)
i dont think there is ever a good reason to call main

You're right, although to be frank, it doesn't matter whether anyone can think of any good reasons or not.

It is illegal to call main from within your code, according to the C++ standard.
Last edited on
Topic archived. No new replies allowed.