where does the value returned by main go ?

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

    where does the value returned by main go ?

    I am wondering where does the value returned by main goes. Suppoes
    main returns some number say 42, where is it stored.

    Supposed a shell script call a C program and the program returns the
    value 42 where is this value stored in memory ?

  • Ian Collins

    #2
    Re: where does the value returned by main go ?

    Daniel Johnson wrote:
    I am wondering where does the value returned by main goes. Suppoes
    main returns some number say 42, where is it stored.
    >
    The value is returned to whatever called main. In an hosted
    environment, that will be the operating system.
    Supposed a shell script call a C program and the program returns the
    value 42 where is this value stored in memory ?
    >
    Well the shell is a program and main is a function to be called, the
    return is simply assigned to a variable in the calling program.

    --
    Ian Collins.

    Comment

    • santosh

      #3
      Re: where does the value returned by main go ?

      DanielJohnson wrote:
      I am wondering where does the value returned by main goes. Suppoes
      main returns some number say 42, where is it stored.
      The value is returned to whatever invoked the program, which in most
      cases is the host operating system.
      Supposed a shell script call a C program and the program returns the
      value 42 where is this value stored in memory ?
      The details vary from system to system, but usually it is made
      available as an ordinary return value for the shell, just like
      functions within your program return values to the caller.

      Comment

      • Keith Thompson

        #4
        Re: where does the value returned by main go ?

        Ian Collins <ian-news@hotmail.co mwrites:
        Daniel Johnson wrote:
        >I am wondering where does the value returned by main goes. Suppoes
        >main returns some number say 42, where is it stored.
        >>
        The value is returned to whatever called main. In an hosted
        environment, that will be the operating system.
        >
        >Supposed a shell script call a C program and the program returns the
        >value 42 where is this value stored in memory ?
        >>
        Well the shell is a program and main is a function to be called, the
        return is simply assigned to a variable in the calling program.
        <OT>
        In a typical shell (at least the kind I'm used to under Unix), a
        program invocation is very different from a function call. The entity
        that actually calls the main() function is not something that's even
        visible to a shell, shell script. or user program.
        </OT>

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Bill Pursell

          #5
          Re: where does the value returned by main go ?

          On Mar 3, 7:12 am, "DanielJohn son" <diffuse...@gma il.comwrote:
          I am wondering where does the value returned by main goes. Suppoes
          main returns some number say 42, where is it stored.
          >
          Supposed a shell script call a C program and the program returns the
          value 42 where is this value stored in memory ?

          On most (all?) *nix systems, the value is stored in the process
          table where it stays until the process that invoked your program (the
          current parent, actually, if the invoking process has already
          terminated) retrieves it via wait().

          --
          Bill Pursell

          Comment

          • Richard Tobin

            #6
            Re: where does the value returned by main go ?

            In article <ln7ityr6j1.fsf @nuthaus.mib.or g>,
            Keith Thompson <kst-u@mib.orgwrote:
            ><OT>
            >In a typical shell (at least the kind I'm used to under Unix), a
            >program invocation is very different from a function call. The entity
            >that actually calls the main() function is not something that's even
            >visible to a shell, shell script. or user program.
            ></OT>
            More OT...

            Typically, an exectable program file will have something in it that
            identifies the address at which the OS should start running it, or
            else the OS will start it at some fixed address. The program that
            creates the executable - the compiler or more likely linker - will
            have placed a function at that address which does any necessary setup
            (dynamic linking for example) and then calls main(). So when main()
            returns, it will return to that function. That function will then
            call exit(), which does things like closing open files and running
            atexit() functions. The parameter passed to exit() is the value
            returned by main(). exit() then terminates the program.

            How does exit() return the value to the shell (or other parent
            program)? Many (perhaps most) operating systems don't have any
            concept of a program just "coming to its end". It can't just run out
            of code, or return in the normal way. If it runs off the end of its
            memory, it will get a signal (e.g. segmentation violation) and the OS
            will terminate it. Something similar is likely to happen if it
            executes a return instruction from the initial function - there won't
            be a useful addess on the stack for it to return to. Instead exit()
            does a system call to tell the OS to terminate it, and it provides its
            argument - the value returned from main() - as an argument to that
            system call. The OS notes this value, and terminates the program.
            Meanwhile, the parent process - such as the shell - has probably
            called a system call that means "wait for my child process to finish".
            That system call now returns with a value corresponding to that
            returned by main() in the child.

            -- Richard
            --
            "Considerat ion shall be given to the need for as many as 32 characters
            in some alphabets" - X3.4, 1963.

            Comment

            • Bill Reid

              #7
              Re: where does the value returned by main go ?


              DanielJohnson <diffuser78@gma il.comwrote in message
              news:1172905938 .839927.197290@ v33g2000cwv.goo glegroups.com.. .
              I am wondering where does the value returned by main goes. Suppoes
              main returns some number say 42, where is it stored.
              >
              It is AVAILABLE to the operating system.
              The operating system is free to do something with it or nothing
              with it...
              Supposed a shell script call a C program and the program returns the
              value 42 where is this value stored in memory ?
              >
              Depends on the operating system. For the great majority of operating
              systems by installation quantity, I don't think they pay any attention to an
              integer returned from main(); it is a hold-over from Unix. However,
              for the great majority of operating systems by installation quantity,
              it is of great importance to return the "windows handle", but since
              main() returns an "int", purely standard-conforming "C" just doesn't
              quite cut it...

              ---
              William Ernest Reid



              Comment

              • E. Robert Tisdale

                #8
                Re: where does the value returned by main go ?

                DanielJohnson wrote:
                I am wondering where does the value returned by main goes. Suppoes
                main returns some number say 42, where is it stored.
                >
                Supposed a shell script call a C program and the program returns the
                value 42 where is this value stored in memory ?
                >
                cat main.c
                int main(int argc, char* argv[]) {
                return 42;
                }
                gcc --version
                gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-51)
                gcc -Wall -std=c99 -O2 -S main.c
                cat main.s
                .file "main.c"
                .text
                .p2align 4,,15
                .globl main
                .type main, @function
                main:
                leal 4(%esp), %ecx
                andl $-16, %esp
                pushl -4(%ecx)
                movl $42, %eax
                pushl %ebp
                movl %esp, %ebp
                pushl %ecx
                popl %ecx
                popl %ebp
                leal -4(%ecx), %esp
                ret
                .size main, .-main
                .ident "GCC: (GNU) 4.1.1 20070105 (Red Hat 4.1.1-51)"
                .section .note.GNU-stack,"",@progb its


                It is implementation dependent. On my system,
                the value is returned in a general purpose register -- eax.

                ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                Comment

                • Randy Howard

                  #9
                  Re: where does the value returned by main go ?

                  On Sat, 3 Mar 2007 20:24:16 -0500, Bill Reid wrote
                  (in article
                  <k5qGh.96057$5j 1.62416@bgtnsc0 4-news.ops.worldn et.att.net>):
                  >
                  DanielJohnson <diffuser78@gma il.comwrote in message
                  news:1172905938 .839927.197290@ v33g2000cwv.goo glegroups.com.. .
                  >
                  >I am wondering where does the value returned by main goes. Suppoes
                  >main returns some number say 42, where is it stored.
                  >>
                  It is AVAILABLE to the operating system.
                  The operating system is free to do something with it or nothing
                  with it...
                  Not the OS typically, but rather the shell, or the calling program,
                  whichever.
                  >Supposed a shell script call a C program and the program returns the
                  >value 42 where is this value stored in memory ?
                  >>
                  Depends on the operating system. For the great majority of operating
                  systems by installation quantity, I don't think they pay any attention to an
                  integer returned from main(); it is a hold-over from Unix.
                  False. Even silly little DOS batch files can handle return values from
                  programs.
                  However, for the great majority of operating systems by installation
                  quantity,

                  *sigh*
                  it is of great importance to return the "windows handle", but since
                  main() returns an "int", purely standard-conforming "C" just doesn't
                  quite cut it...
                  It's not important to return a "windows handle". Literally thousands
                  of Windows executables get by just fine without doing so.


                  --
                  Randy Howard (2reply remove FOOBAR)
                  "The power of accurate observation is called cynicism by those
                  who have not got it." - George Bernard Shaw





                  Comment

                  • Walter Roberson

                    #10
                    Re: where does the value returned by main go ?

                    In article <0001HW.C221B03 400C54093F02036 48@news.verizon .net>,
                    Randy Howard <randyhoward@FO OverizonBAR.net wrote:
                    >On Sat, 3 Mar 2007 20:24:16 -0500, Bill Reid wrote
                    >(in article
                    ><k5qGh.96057$5 j1.62416@bgtnsc 04-news.ops.worldn et.att.net>):
                    >DanielJohnso n <diffuser78@gma il.comwrote in message
                    >news:117290593 8.839927.197290 @v33g2000cwv.go oglegroups.com. ..
                    >>I am wondering where does the value returned by main goes. Suppoes
                    >>main returns some number say 42, where is it stored.
                    >It is AVAILABLE to the operating system.
                    >The operating system is free to do something with it or nothing
                    >with it...
                    >Not the OS typically, but rather the shell, or the calling program,
                    >whichever.
                    However, the OS gets it first and makes it available to the shell.

                    For example in Unix, the program return code is typically converted
                    to an unsigned octet and that octet is typically 8 bits
                    from the end of the structure that is made available to the calling
                    process (such as a shell).

                    That truncation to 8 bits and insertion into a larger structure
                    constitutes "doing something with it"... even if it's always
                    the -same- thing no matter what the value :)
                    --
                    Programming is what happens while you're busy making other plans.

                    Comment

                    Working...