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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With