Is it legal to recurse into main() in C++?



In C or C++, the main function is like other functions. So we can use the functionalities that are present in some other functions, also in the main function.

In the following program, we will see how the main() is using recursively to print some numbers in reverse order.

Example Code

#include <iostream>
using namespace std;
int main () {
   static int x = 10;
   cout << x-- << endl;
   if(x) {
      main();
   }
}

Output

10
9
8
7
6
5
4
3
2
1

Advertisements