11

I heard that in C, main() is reentrant, while in C++ is not.

Is this true? What is the scenario of re-entering the main() function?

Deqing
  • 14,098
  • 15
  • 84
  • 131
  • 7
    I don't think you mean [reentrant](http://en.wikipedia.org/wiki/Reentrancy_(computing)). It is true however that you are allowed to call `main` in C and you are not allowed to call it in C++. – David Brown Jun 27 '13 at 03:05

1 Answers1

16

Early C++ implementations, which were based on translation to C, implemented global constructors via adding a function call to the beginning of main. Under such an implementation, calling main again would re-run the global ctors, resulting in havoc, so it was simply forbidden to do so.

C on the other hand had no reason to forbid calling main, and it was always traditionally possible.

As for when it's useful, I would say "rarely". Most of the programs I've seen that called main were IOCCC entries.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711