Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recursion of main

Tags:

c++

recursion

I read some where that recursion of main() is not allowed in c++ but when i tried it ran without any error

#include<iostream>
using namespace std;

int i=10;

int main()
{
    if(i==1)
    {
        cout<<i;
        return 0;
    }
    i--;
    main();
}
like image 357
user1484638 Avatar asked Dec 20 '22 17:12

user1484638


1 Answers

Calling main explicitly is undefined behavior, anything can happen (including appear to work).

C++03 3.6.1

3) The function main shall not be used within a program. [...]

The compiler (as all undefined behavior goes) is not required to provide a diagnostic, nor is the runtime required to crash.

like image 144
Luchian Grigore Avatar answered Dec 24 '22 02:12

Luchian Grigore