how main() is called in C programme

Discussion in 'C Programming' started by chang, Apr 27, 2008.

  1. chang

    chang Guest

    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
    chang, Apr 27, 2008
    #1
    1. Advertising

  2. 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.
    Antoninus Twink, Apr 27, 2008
    #2
    1. Advertising

  3. In article <>,
    chang <> 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
    Walter Roberson, Apr 27, 2008
    #3
  4. Antoninus Twink <> 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.
    Eligiusz Narutowicz, Apr 27, 2008
    #4
  5. chang

    santosh Guest

    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>
    santosh, Apr 27, 2008
    #5
  6. chang

    chang Guest

    On Apr 27, 11:43 am, 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 .
    >
    > 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
    chang, Apr 27, 2008
    #6
  7. chang

    santosh Guest

    chang wrote:

    > On Apr 27, 11:43 am, 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 .

    [ ... ]
    > 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>
    santosh, Apr 27, 2008
    #7
  8. chang <> writes:
    > On Apr 27, 11:43 am, chang <> 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) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 27, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John M
    Replies:
    1
    Views:
    1,133
    Kumar Reddi
    May 29, 2005
  2. The Doctor
    Replies:
    0
    Views:
    269
    The Doctor
    Oct 9, 2003
  3. The Doctor
    Replies:
    3
    Views:
    336
    Peter Hansen
    Oct 10, 2003
  4. Ravi
    Replies:
    17
    Views:
    924
    Kenneth Brody
    Apr 1, 2006
  5. main() called inside main()

    , May 4, 2006, in forum: C Programming
    Replies:
    14
    Views:
    1,425
    Richard Heathfield
    May 7, 2006
Loading...

Share This Page