7

Possible Duplicate:
Can main function call itself in C++?

I found this problem very interesting but illusive a bit. Question 6.42 C++ how to program by Dietel "Can main be called recursively on your system? write a program containing a function main. Include Static local variable count and initialize to 1. Post-increment and print the value of count each time main is called. Compile your program. What happens ?

I wrote the program as below but instead I made the recursion stops after 10 times as if I were to keep it running it will stops at a value around 41000.

my question: how is it legal to call recursively main function in c++, should this program be executed to stack over flow or memory fault, etc.. ? Please explain.

#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

Community
  • 1
  • 1
Sinan
  • 453
  • 2
  • 10
  • 20

1 Answers1

21

How is it legal to call the main() function recursively in C++

It is not legal. The C++ language standard states that "The function main shall not be used within a program" (C++11 §3.6.1/3). Calling the function is a form of "use."

Any program that calls main() exhibits undefined behavior (technically, such a program is ill-formed because the rule being violated is a diagnosable semantic rule, though I'd be surprised if most compilers rejected the program). Note that this does not prevent the runtime infrastructure that starts your program from calling the main() function.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 3
    I think it also helps to quote the more human-readable, but less general "Recursive calls are permitted, except to the function named main" from §5.2.2[expr.call]/9 – Cubbi Jul 05 '12 at 17:40
  • @James, thank you for your reply - if the C++ standard states the above - so why the recursive call is permitted by compilers. I found behavior weird to comprehend. I would appreciate your reply. – Sinan Jul 05 '12 at 17:42
  • 1
    I cannot answer that for all compilers, since I am only very familiar with one (Visual C++) and somewhat familiar with a few others. However, I would hazard to guess that `main()` is just treated by the compiler like any other function. On Windows, `main()` is not the entry point called by the OS: it couldn't be, because the C runtime has to do work to initialize globals and prepare the environment for execution. After it does this, the C runtime calls your program's `main()`. So, it really is just another function, but _you_ are not allowed to call it, only the C runtime may call it. – James McNellis Jul 05 '12 at 17:46
  • Thank you James, that explains alot. – Sinan Jul 05 '12 at 17:49
  • 5
    @Sinan: Undefined behavior is a get out of jail free card to compiler vendors. It means vendors have free reign to do whatever they want and still be compliant with the standard. Many vendors use a common framework for their C and C++ compilers. The C standard requires vendors to provide the ability to call `main` recursively. The easiest thing to do is to extend that capability to C++. That's OK by the C++ standard. Calling main recursively in C++ might work on your computer with your compiler because of this. That does not mean it will work when you switch computers / switch compilers. – David Hammen Jul 05 '12 at 17:50