Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how main() is called in C programme

Reply
Thread Tools

how main() is called in C programme

 
 
chang
Guest
Posts: n/a
 
      04-27-2008
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
 
Reply With Quote
 
 
 
 
Antoninus Twink
Guest
Posts: n/a
 
      04-27-2008
On 27 Apr 2008 at 18:43, chang wrote:
> 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.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      04-27-2008
In article <(E-Mail Removed)>,
chang <(E-Mail Removed)> wrote:

> 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
--
"I like to build things, I like to do things. I am having
a lot of fun." -- Walter Chrysler
 
Reply With Quote
 
Eligiusz Narutowicz
Guest
Posts: n/a
 
      04-27-2008
Antoninus Twink <(E-Mail Removed)> writes:

> On 27 Apr 2008 at 18:43, chang wrote:
>> 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.


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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      04-27-2008
chang wrote:

> 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>

 
Reply With Quote
 
chang
Guest
Posts: n/a
 
      04-27-2008
On Apr 27, 11:43*am, chang <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      04-27-2008
chang wrote:

> On Apr 27, 11:43*am, chang <(E-Mail Removed)> wrote:
>> 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>

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-27-2008
chang <(E-Mail Removed)> writes:
> On Apr 27, 11:43*am, chang <(E-Mail Removed)> wrote:
>> * * * * * *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.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
What is called carry chain structure in FPGA is called in IC? Weng Tianxiang VHDL 6 09-12-2007 07:19 AM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM
Re: Problems with a programme called CyberChair The Doctor Python 3 10-10-2003 04:46 PM
Problems with a programme called CyberChair The Doctor Python 0 10-09-2003 02:09 PM



Advertisments