Why calling main is not allowed in C++ when it is allowed in C? [duplicate]

  • A+
Category:Languages

This question already has an answer here:

Triggered by this question I was trying to find something in the gcc documentation on what happens when I call main in C++. I did not find anything, but found out that in C its just fine to call main. Note that

#include <iostream> int main() {     int x;      std::cin >> x;     if (x) main(); } 

is not valid in C++, though it compiles unless using the -pedantic flag. However, calling main is allowed in C.

What is different in C++ that requires to disallow calling main?

 


C++ requires constructors for static objects to be called - this is may be implemented in the call to main. If you could call main, the constructors might be run multiple times. C doesn't have constructors, so doesn't have the same issues.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: