Recursive call to main C ++

Possible duplicate:
Can a main function call itself in C ++?

I found this problem very interesting, but illusory. Question 6.42 C ++, how to program Dietel "Can you call main recursively on your system? Write a program containing the main function. Turn on the Static local variable count and initialize to 1. Gradually increment and print the count value each time main is called. programs What is happening?

I wrote a program as shown below, but instead I made recursion stops 10 times, as if I supported it, it would stop with a value of about 41000.

my question is: how to legally call the main function recursively in C ++, should this program be executed for the stack by thread or memory error, etc.? Explain, please.

#include <iostream> using namespace std; int main() { static int count = 0; count++; if(count <= 10) { cout << count << endl; return main(); //call main }//end if system("pause"); return 0;//successful completion }//end main 

Thank you

+6
source share
1 answer

How to legally call the main() function recursively in C ++

It is illegal. The C ++ language standard states that β€œthe main function should not be used within the framework of a program” (C ++ 11 Β§3.6.1 / 3). A function call is a form of "use."

Any program that calls main() exhibits undefined behavior (technically, such a program is poorly formed because the rule being violated is a diagnostic semantic rule, although I would be surprised if most compilers reject the program). Note that this does not prevent the runtime infrastructure from running your program from a call to main() .

+17
source

Source: https://habr.com/ru/post/919787/


All Articles