where does /return value/ go?

A

arnuld

1.) we usually say /return 0/ indicates success e.g

#include <iostream>

int main() {
int i=3;

std::cout << i << std::endl;

return 0;
}


i ran the programme & i see only /3/ as output. i dont see any /0/ on
my terminal. where does it go? & who reads /0/ to indicate success?
 
S

Steve Pope

arnuld said:
1.) we usually say /return 0/ indicates success e.g

#include <iostream>

int main() {
int i=3;

std::cout << i << std::endl;

return 0;
}


i ran the programme & i see only /3/ as output. i dont see any /0/ on
my terminal. where does it go? & who reads /0/ to indicate success?

Programs such as shells and make utilities can see and use the return
value of a program. A user executing the program from the command
line does not usually see it.

You'll have to look at your operating system documentation to
learn more.

Steve
 
G

Gianni Mariani

arnuld said:
1.) we usually say /return 0/ indicates success e.g

#include <iostream>

int main() {
int i=3;

std::cout << i << std::endl;

return 0;
}


i ran the programme & i see only /3/ as output. i dont see any /0/ on
my terminal. where does it go? & who reads /0/ to indicate success?

If you executed your program from the std::system function call then the
value is partly returned by the system call. If the return value from
system() is negative then there was an error, otherwise the lower 8 bits
are the lower 8 bits returned from main often termed the "status".

Other facilities like "pclose", createprocess, waitpid, etc also enable
getting at the "status" in the same way.

The convention is to return 0 if the program was "successful" and non
zero otherwise. I a compiler finds an error, it is expected that the
return status is "1". For example, in the "sendmail" the mail system,
if the program has a "temporary" error, the return status should be "75"
or sometimes termed EX_TEMPFAIL.

see:
http://www.google.com/codesearch?q=...erver_sasl-59.3/cyrus_sasl/include/exits.h#a0

You almost never have to worry about anything other than "0" - success,
or "1" failure.
 
J

Jacek Dziedzic

arnuld said:
1.) we usually say /return 0/ indicates success e.g

#include <iostream>

int main() {
int i=3;

std::cout << i << std::endl;

return 0;
}


i ran the programme & i see only /3/ as output. i dont see any /0/ on
my terminal. where does it go? & who reads /0/ to indicate success?

<OT>
Your command shell took it. Usually you'll have an OS-specific
means to check the return value, e.g. in a bash shell under
linux you may try, assuming your program executable is named a.out

#!/bin/bash
../a.out
echo "The return value is $?"

</OT>

HTH,
- J.
 
D

Default User

Gianni Mariani wrote:

If you executed your program from the std::system function call then
the value is partly returned by the system call. If the return value
from system() is negative then there was an error, otherwise the
lower 8 bits are the lower 8 bits returned from main often termed the
"status".

There is no such requirement for the system() function. From the C99
draft standard:

7.20.4.5 The system function

Synopsis

[#1]

#include <stdlib.h>
int system(const char *string);

Description

[#2] If string is a null pointer, the system function
determines whether the host environment has a command
processor. If string is not a null pointer, the system
function passes the string pointed to by string to that
command processor to be executed in a manner which the
implementation shall document; this might then cause the
program calling system to behave in a non-conforming manner
or to terminate.

Returns

[#3] If the argument is a null pointer, the system function
returns nonzero only if a command processor is available.
If the argument is not a null pointer, and the system
function does return, it returns an implementation-defined
value.





Brian
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top