Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Where the main() function exists in c++ ? Is it a user defined function?
Posted

See the definition here[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 15:01pm    
Good answer, a 5.
--SA
See, for instance "Main function" at Wikipedia[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 15:01pm    
Good answer, a 5.
--SA
"A program must contain a function called main(). This function is the designated start of the program. The function is not predefined by the compiler, it cannot be overloaded, and its type is implementation dependent." The Annotated C++ Reference Manual, M. A. Ellis & B. Stroustrup, Addison-Wesley, 1986, Section 3.4.

Indeed, it is a user-defined function. You will find it somewhere in the source code, in a .cpp file (+ see Solution 4).
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 15:01pm    
Good answer, a 5.
--SA
The only reason why main() exist in a C++ program is to retain a C backward compatibility.

A C++ program can even be written without main does anything.
See here[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 14:56pm    
Great point! Finally I see the answer from someone who really knows the essence of things :-). My 5.
However, I must note the function "main" still has to exist.
--SA
Richard MacCutchan 3-Aug-11 15:49pm    
But it cannot be written without main, as it says in that link, main() has to be there.
Emilio Garavaglia 3-Aug-11 16:22pm    
Unfortunately yes.
The point is that C++ is not "pure OOP", not "pure generic" not "pure procedural", not "pure functional".
If you try to be zealot on one of those paradigms, you find "dirtiness" because of the bridges towards the others.
In a basic C++ (console) the main (or one of its variant line _tmain,... ) function need to exists, it will be called by the program loader.

In a GUI program (MFC/QT/...) , the main will be hidden in the framework; the framework will then call its own "main" function (for example WinMain, which might still be hidden btw).

M.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 14:58pm    
Well, I would rather say "implemented" instead of "hidden". Correct, my 5. See also the answer by Emilio -- important point.
--SA
Emilio Garavaglia 3-Aug-11 16:27pm    
It is not called by the "program loader", but from the program initialization routine whose name is defined in the linker configuration (the default for Micorsoft linkers is mainCRTStartup).
It calls all global object constructors, then main, then the local static destructors, then the global object destructors.
No, It is a entery point function. which defination is defined by programer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900