9

Possible Duplicates:
restrictions on the main() function
Is it legal to recurse into main() in C++?

I read in C++ Primer that main is not allowed to be called recursively, and in some related questions here on SO it is indeed confirmed that it is illegal.

But why is it illegal? As long as you avoid a stack overflow, what's the problem with calling main within itself?

Community
  • 1
  • 1
gablin
  • 4,678
  • 6
  • 33
  • 47
  • Out of curiousity, what happens if you do? – Rob Agar Mar 16 '11 at 14:10
  • If your compiler complains, just move all the code inside `main()` to another function and have `main()` just call that function (which then calls itself recursively). Problem solved. – Platinum Azure Mar 16 '11 at 14:11
  • http://stackoverflow.com/questions/4518598/is-it-legal-to-recurse-into-main-in-c/4519407#4519407 goes into some of the possible/potential reasons. – Joe Mar 16 '11 at 14:13
  • Why on earth would you want to. If it comes down to it, you can call a program using exec() or fork() – deek0146 Mar 16 '11 at 14:30
  • 1
    None og the linked topics try to elaborate on the WHY. They just copy-paste the ANSI C documentation. IMO it has hystorical reasons. In C you can write programs for many-many different IC-s and low level elements. Keping the entry point special may e important to low-level implementations which has no particular init code called before `main()`, or calling that particular address aso puts the CPU into a special *program init* state. Just a speculation, but it sounds plausible according to the little exprience in the topic. – vbence Mar 16 '11 at 14:31
  • There is a very good historic reason for this decision. It was intended that C++ could be implemented as a sort of preprocessor/translation to C, and in that translation process, a call to all global constructors would be added at the beginning of `main`, followed by a call to `atexit` to install an exit handler that would call all global destructors. Recursive calls to `main` would then cause these constructors to be called more than once. – R.. GitHub STOP HELPING ICE Sep 02 '11 at 22:36

2 Answers2

1

Well, the standard states:

3.6.1.3 "The function main shall not be used within a program."

5.2.2.9 "Recursive calls are permitted, except to the function named main"

I guess it is beause main() is a special function used as the entry point to the program. I'd say keep it special, don't take it down the level of a normal function, because it is not.

user662630
  • 85
  • 2
  • 5
    Quoting the standard doesn't answer the question. The asker wants to know why the standard says that. – Karu Mar 26 '12 at 10:06
-1

I believe that the wording in 3.6.1/3 forbids this, saying it shall not be used in a program:

The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementation defined. A program that declares main to be inline or static is illformed. The name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. ]

Then in 3.2/2

An object or nonoverloaded function is used if its name appears in a potentially evaluated expression.

This clearly indicates that used includes possible calls (which would be recursive) to main.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • 2
    Quoting the standard doesn't answer the question. The asker wants to know why the standard says that. – Karu Mar 26 '12 at 10:07
  • @Karu If the OP wants to know an answer more detailed than "the standard says so, presumably to simplify implementations" Stackoverflow is the wrong place to ask that question. – Mark B Mar 26 '12 at 13:52
  • Fair enough. Actually I have no idea what the OP wanted. I've just been frustrated trying to find justifications for things in the standard (so I can understand/remember it better) and instead finding heaps of "because the standard says so" answers. – Karu Mar 27 '12 at 19:08