Link to home
Start Free TrialLog in
Avatar of rajesh_bala
rajesh_balaFlag for India

asked on

who calls main()?

main() is a fuction.It is called when the program is executed.But if it is a function, then someone else should call it.Who calls the main() function?Please explain in detail..
Avatar of loumf
loumf

It depends on your compiler.  That is, it is not specified by the language.  If you really need to know then put a break point at main() in your debugger and look at the call stack.
In general, your compiler generates code to include with all of your code that bridges the gap between how the OS wants to run executables and main().
The compiler basically converts the high level stuff into machine readable form something like assembly. The way that you execute your program is load these instruction sets into memory and ask the processor to execute from some starting address. What the c compiler does it to make the corresponding machine code in the main function as the starting point for execution.
There is a header in each executable.  Among other things, it defines the starting point for a program.  For C programs, this is always the address of the main function.
main is called by _main(...) of C++ usually
As other experts have said, when a program is loaded it contains information that indicates where its "entry point", or initial function is, and the operatign system simply calls this function.

However in C and C++ programs the entry point is not main().  The entry point is a procedure that the compiler provides.  This procedure will perform some initializations, it will initialize the C/C++ run-time library.  This involves opening the 3 standard I/O handles and perhaps initializaing some global data structures.  Also the heap will be set up.   There may be other initializations to be done as well.   In a C++ program, the entry point function will probalby also call the constructors for all the global objects defined in the program.   Then the entry point function will call your main().
nietod is referring to what is known as "startup code"...
the entry point function nietod referring is usually something like I mentioned preivously _main() of the compiler
C++ compilers provide you with the startup routine that calls the main function.

If you are using Visual C++, take a look at the C Run-Time Library function source code - crt0.c, which contains the actual startup routine.
>> the entry point function nietod referring is
>> usually something like I mentioned
>> preivously _main() of the
>> compiler
The _main() you see is the main() that you wrote.  Underscores are usually prepended to function names of functions with "c" linkage on most implimentations.    
In VC++ write "main" in link->output->entry-point symbol, and use /NODEFAUTLIB flag.
Now you are sure that your main function is the first.
But do not try to call C runtime library functions - use only API. You will get really small Win Application.
The question isn't asking how to make main() the entry point.  making main() the entry point is usually not a good idea.
ASKER CERTIFIED SOLUTION
Avatar of danelroisman
danelroisman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
After the long haul of reading this, the part of it that is relevant is saying that same as nietod did.

That is, the program has a startup routine that parses the command line, and does other initialisations then calls you main function, and tidies up at the end.

There is no stadard function that does this.  The actual routine is compiler / library / linker dependant - not even just OS dependant.  Unless you go to the trouble of defining your own runtime library, you will ususally get teh standard compiler/library/linker supplied startup routine.

I think nietod deserves the points for first answer the question (what calls main).  His answer was succinct and understandable .. lathough the information is buried in the article above (if you read carefully) the article really doesn't say anything much more in answer to the original question (but is quite useful if you want to do your own startup code for ultimate size/speed for smaller aps).
Thanks for the support.