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?
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?
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.