Re: how main() is called in C programme

Hi ALL,
I am working in C from past few months. Still now i can't
figure out who is called main() in 'C' programme?
Main() is a function from that we can call our sunroutines but someone
has to call this Main() and it will return to whom and in runtime
this Main() is stored/runnng exactly where in the memroy ?

I will appreciate if someone help me to figure it out .

Under "hosted" systems the so-called "host environment" is responsible
for invoking main (note: C is case sensitive. main and Main are
different identifiers). When the program is done control is given back
to the host environment in a implementation defined fashion. One common
candidate for the host environment is the operating system. Examples
are Linux, Solaris, Windows, DOS etc.

Things are often very different for the so-called "freestanding"
systems. Here the program entry point need not be named main at all.
Also very often there is no host environment. The program is directly
invoked by the hardware and normally terminates only when the device is
switched off. Under such environments of course it might not make sense
to return from the main function (or whatever it's called) or to invoke
exit, _Exit or similar.

Details of course vary enormously. Some embedded systems do have
complete operating systems and allow execution of normal hosted C
programs. Note that the host software itself is a (if written in C)
freestanding program.

The details of how exactly control is given to a hosted C program and
what happens after such a program terminates are *very* system
dependent. It will vary depending on the processor, operating system
and your C implementation.

Often the case is that your C compiler will prefix your program with a
small amount of code which is called by the host system when your
program is run. This code may do various housekeeping duties like
opening the predefined streams (stdin, stdout and stderr), initialising
the malloc subsystem, setting up the commandline arguments to main,
setting up default signal handlers, setting your default locale and
other such tasks before invoking main as a normal function call.

Similarly when your program terminates control may be passed to code
that's linked in with your program by the compiler. This often resides
in implementation defined functions called by exit. Often this is the
function _Exit. It closes open streams, flushes unwritten output and
calls any functions registered with atexit before transferring control
back to the host environment, usually through a system call.

Please ask in a system specific group like comp.unix.programmer or
comp.os.ms-windows.programmer.win32 for more details. Also reading the
source for a C library implementation like glibc might be illuminating.

The terms used in this post like hosted, freestanding etc. are all
defined in the ISO standard for C, a public draft of which can be
obtained at:

Re: exit() values and the C standards?...pete wrote: ... ways to signal successful termination, so long as the host environment provides at least two such ways. ... In some environments, all odd values returned to the host environment indicate different kinds of success, and all even ones indicate different kinds of failure. ... On such a system, EXIT_SUCCESS could be 1, EXIT_FAILURE could be 2, and a value of 0 passed to exitwould cause the host environment to receive a value of 3. ...(comp.lang.c)