By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,995 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,995 IT Pros & Developers. It's quick & easy.

where does the value returned by main go ?

P: n/a
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 ?

Mar 3 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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.
Mar 3 '07 #2

P: n/a
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.

Mar 3 '07 #3

P: n/a
Ian Collins <ia******@hotmail.comwrites:
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_Keith) ks***@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"
Mar 3 '07 #4

P: n/a
On Mar 3, 7:12 am, "DanielJohnson" <diffuse...@gmail.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

Mar 3 '07 #5

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 3 '07 #6

P: n/a

DanielJohnson <di********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.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

Mar 4 '07 #7

P: n/a
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,"",@progbits
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 =----
Mar 4 '07 #8

P: n/a
On Sat, 3 Mar 2007 20:24:16 -0500, Bill Reid wrote
(in article
<k5*******************@bgtnsc04-news.ops.worldnet.att.net>):
>
DanielJohnson <di********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.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

Mar 17 '07 #9

P: n/a
In article <00*****************************@news.verizon.net> ,
Randy Howard <ra*********@FOOverizonBAR.netwrote:
>On Sat, 3 Mar 2007 20:24:16 -0500, Bill Reid wrote
(in article
<k5*******************@bgtnsc04-news.ops.worldnet.att.net>):
>DanielJohnson <di********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googleg roups.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.
Mar 17 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.