how main() is called in C programme

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chang

    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 .

    Looking forward to .

    Thanks

    Chinmoy
    you
  • Antoninus Twink

    #2
    Re: how main() is called in C programme

    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:


    Ask if you have any questions about it.

    Comment

    • Walter Roberson

      #3
      Re: how main() is called in C programme

      In article <f1d4c85a-18ae-4f9a-b07d-388181a7b477@l1 7g2000pri.googl egroups.com>,
      chang <chinmoy.chitta ranjan@gmail.co mwrote:
      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


      --
      "I like to build things, I like to do things. I am having
      a lot of fun." -- Walter Chrysler

      Comment

      • santosh

        #4
        Re: how main() is called in C programme

        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 "freestandi ng"
        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.progr ammer or
        comp.os.ms-windows.program mer.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>

        Comment

        • chang

          #5
          Re: how main() is called in C programme

          On Apr 27, 11:43 am, chang <chinmoy.chitta ran...@gmail.co mwrote:
          Hi ALL,
                     I am working in C from past few months. Still now ican'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

          Comment

          • santosh

            #6
            Re: how main() is called in C programme

            chang wrote:
            On Apr 27, 11:43 am, chang <chinmoy.chitta ran...@gmail.co mwrote:
            >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.p rogramming.apps or comp.unix.progr ammer
            or comp.os.ms-windows.program mer.win32 or a group in the
            microsoft.publi c.* 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.tx t>

            <snip>

            Comment

            • Keith Thompson

              #7
              Re: how main() is called in C programme

              chang <chinmoy.chitta ranjan@gmail.co mwrites:
              On Apr 27, 11:43 am, chang <chinmoy.chitta ran...@gmail.co mwrote:
              >           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.prog rammer, comp.os.ms-windows.program mer.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_Keit h) <kst-u@mib.org>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              Working...