how main() is called in C programme

C

chang

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 .

Looking forward to .

Thanks

Chinmoy
you
 
A

Antoninus Twink

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 ?

There's an informative article on this question for gcc/Linux here:
http://linuxgazette.net/issue84/hawk.html

Ask if you have any questions about it.
 
W

Walter Roberson

chang said:
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 ?

The details of how main() is called are system dependant.

For a description of how one particular Unix system does it,
and keeping in mind that the details might differ on other unix
systems, see the reference to crt0.o in

http://docs.hp.com/en/B2355-90968/compileandlink.htm
 
E

Eligiusz Narutowicz

Antoninus Twink said:
There's an informative article on this question for gcc/Linux here:
http://linuxgazette.net/issue84/hawk.html

Ask if you have any questions about it.

This is good article on the subject - thank you. I think it is important
for C programmers to know how real C systems is working for at this in
order to better utilise the HW and system.
 
S

santosh

chang said:
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:

<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>
 
C

chang

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 .

Looking forward to .

Thanks

Chinmoy
you

Thanks for all responses but is there any straight forward
documenatation/link is there so that it can guide me in a disciplined
way?

Again looking forward to you guys.

Thanks
Chinmoy
 
S

santosh

chang said:
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 .
[ ... ]
Thanks for all responses but is there any straight forward
documenatation/link is there so that it can guide me in a disciplined
way?

The details vary from system to system so there is no one piece of
documentation that will inform you. Some posters have already given you
some links. For further help you should post to a group that deals with
your system like comp.os.linux.programming.apps or comp.unix.programmer
or comp.os.ms-windows.programmer.win32 or a group in the
microsoft.public.* hierarchy. The details of how your program is called
and how it exits is also heavily influenced by your compiler and C
standard library implementations, in addition to your operating system.
Again try in a compiler specific group. There is simply no universal
answer.

Here is one link that is x86/UNIX specific:

<http://fgiasson.com/articles/memorylayout.txt>

<snip>
 
K

Keith Thompson

chang said:
           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 .
[...]
Thanks for all responses but is there any straight forward
documenatation/link is there so that it can guide me in a disciplined
way?

The C language doesn't have a whole lot to say about how main() is
invoked. To see just what it does say, download
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (the
latest semi-official draft of the C standard) and read section 5.1.2,
"Execution environments".

A quick summary:

In a hosted implementation, the main() function is invoked by the
execution environment. Certain things need to be set up before main()
begins executing. The details of how this is done are left up to the
implementation. Under a freestanding implementation (typically, but
not necessarily, an embedded system), the requirements are even
looser; the program entry point might not be called "main".

For more details about what happens in the implementation you're
using, ask in an implementation-specific newsgroup
(comp.unix.programmer, comp.os.ms-windows.programmer.win32, etc.).

Why do you want to know? That's not meant to imply that you shouldn't
be curious, but for portable programming you really don't *need* to
know these details. Your program will work as specified regardless of
where your main function happens to be in memory or how it's invoked.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top