Is there a reason to forward declare main in C++

Viewed 82

I'm going through some code from someone who has left the company and there are forward declarations of main() in many of the application source files. I have never seen this in C, which is my main language.

Is there a reason in C++, or C for that matter, to forward declare main()?

1 Answers

Calling main() directly in C++ is not allowed so there is no real need to forward declare it. A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier.

In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function’s body. This way, when the compiler encounters a call to the function, it’ll understand that we’re making a function call, and can check to ensure we’re calling the function correctly, even if it doesn’t yet know how or where the function is defined.

So you don't have to worry about the forward declared main. If you want you can just clear them off.

Related