472,992 Members | 3,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

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 ?

Mar 3 '07 #1
9 3662
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
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
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
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Barry Edmund Wright | last post by:
I would really appreciate your assistance. I am using Access 2000 to create a form that Lists Names and Addresses based on a number of selection criteria one of which is a combo box cboPCZip. All...
44
by: Bamber | last post by:
Why does f get returned ? (compiled with gcc). #include<stdio.h> int func(int a); main() { int f; f=func(7); printf("f=%d",f);
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
7
by: Microsoft | last post by:
I'm not sure where to physically place my subroutines in vb.net I get namespace and not declared errors... Imports System Imports System.Management Public Class Form1
19
by: centurian | last post by:
I have compiled following program with g++/gcc 3.3.1 and "MS Visual C++ 6" and it ran without any errors. Does this thing make any sense? Is it of some use or some problem with grammar/CFG of...
4
by: arnuld | last post by:
1.) we usually say /return 0/ indicates success e.g #include <iostream> int main() { int i=3; std::cout << i << std::endl; return 0;
12
by: Gagan | last post by:
Hi All, I am new to VB.NET, and am confused by following code. The code basically obtains an instance of an object from a helper method. This helper method instantiates a new object, and returns...
7
by: krishna81m | last post by:
In the following code snippet, I created a class samp which has two private variables /char/ and /char*/ which are initialized in the constructor. I create a function /samp input()/ that returns an...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.