indication

N

nomadcontauroi

#include <stdio.h>
#include <math.h>

int main(void)
{
int number = -1234;

printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}





what does the "return 0" indicate in the above program?
thanks.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

#include <stdio.h>
#include <math.h>

int main(void)
{
int number = -1234;

printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}





what does the "return 0" indicate in the above program?
thanks.

It indicates that the main() function explicitly returns a value of zero to it's
caller. What the caller does with this return value depends on the nature of the
caller.


- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDz5SdagVFX4UWr64RArWNAJ92HjC3hkFkFb12SXsygeXJ7YNh6ACg5bKA
OBEbSUzKAmEeywrj2UbE51U=
=hyvF
-----END PGP SIGNATURE-----
 
V

Vladimir S. Oka

#include <stdio.h>
#include <math.h>

int main(void)
{
int number = -1234;

printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}





what does the "return 0" indicate in the above program?
thanks.

It indicates a success, i.e. the program finished with no errors.

This value is intended to be checked by the host environment (e.g. a
shell script) to determine the outcome of the program run. Standard C
also defines it as EXIT_SUCCESS macro. There's also EXIT_FAILURE
(defined as 1, with obvious meaning), and the two are the only values
standard knows about, i.e. the only portable values to return to the
environment. To use either, you have to #include <stdlib.h>.

Cheers

Vladimir
 
X

Xiaodong Xu

(e-mail address removed) writes:

each function has a return value, so does the 'main'
function.

you'd better have each function with a return value
so that the caller could learn the result of the function
called.

in common case, the return value '0' means the function
is executed suceessfully. of course you may define other
error codes for it if anything abnormal occurs.
 
F

Flash Gordon

Vladimir said:
It indicates a success, i.e. the program finished with no errors.
Yes.

This value is intended to be checked by the host environment (e.g. a
shell script) to determine the outcome of the program run. Standard C
also defines it as EXIT_SUCCESS macro. There's also EXIT_FAILURE
(defined as 1, with obvious meaning),

No. EXIT_FAILURE is defined with some value, not necessarily 1. On at
least some systems returning 1 will indicate *success*, and on such a
system it is highly doubtful that EXIT_FAILURE would be defined as 1.
> and the two are the only values
standard knows about, i.e. the only portable values to return to the
environment. To use either, you have to #include <stdlib.h>.

To be clear, the portable values a 0, EXIT_SUCCESS and EXIT_FAILURE,
with 0 meaning success and the other two are obvious. A return value of
1 is most definitely *not* portable.
 
E

Eric Sosman

Vladimir said:
[...]
what does the "return 0" indicate in the above program?
thanks.

It indicates a success, i.e. the program finished with no errors.

This value is intended to be checked by the host environment (e.g. a
shell script) to determine the outcome of the program run. Standard C
also defines it as EXIT_SUCCESS macro. There's also EXIT_FAILURE
(defined as 1, with obvious meaning), and the two are the only values
standard knows about, i.e. the only portable values to return to the
environment. To use either, you have to #include <stdlib.h>.

Vladimir is partly correct, but not entirely.
EXIT_SUCCESS is not necessarily zero and EXIT_FAILURE
is not necessarily one. (If they were always zero and
one, the macros would be fairly useless ...).

On a host system that has no notion of the success
or failure of a program, it is even conceivable that
EXIT_FAILURE == EXIT_SUCCESS. (Note that I wrote
"conceivable," not "likely.")

Summary: zero means "success," EXIT_SUCCESS means
"success" (possibly a different flavor), and EXIT_FAILURE
means "failure" -- and these three (or two, or one) values
are the only termination codes blessed by the Standard.
 
V

Vladimir S. Oka

Eric said:
Vladimir is partly correct, but not entirely.
EXIT_SUCCESS is not necessarily zero and EXIT_FAILURE
is not necessarily one. (If they were always zero and
one, the macros would be fairly useless ...).

On a host system that has no notion of the success
or failure of a program, it is even conceivable that
EXIT_FAILURE == EXIT_SUCCESS. (Note that I wrote
"conceivable," not "likely.")

Summary: zero means "success," EXIT_SUCCESS means
"success" (possibly a different flavor), and EXIT_FAILURE
means "failure" -- and these three (or two, or one) values
are the only termination codes blessed by the Standard.

Thanks Eric, you're right. I stand corrected.

Cheers

Vladimir
 
K

Keith Thompson

Flash Gordon said:
To be clear, the portable values a 0, EXIT_SUCCESS and EXIT_FAILURE,
with 0 meaning success and the other two are obvious. A return value
of 1 is most definitely *not* portable.

And since 0 is required to indicate success, there's probably no good
reason for an implementation to define EXIT_SUCCESS as anything other
than 0 -- but it could, and you shouldn't assume that EXIT_SUCCESS==0.
(There's little reason to make such an assumption anyway.)

IMHO, the standard would have been clearer and more consistent if it
had *not* required 0 to indicate success, just defining EXIT_SUCCESS
and EXIT_FAILURE with implementation-specific values. The requirement
causes some problems for VMS^H^H^H OpenVMS, where odd values denote
success and even values denote errors; the C runtime system has to
make special allowances for "return 0;".

But I suppose "return 0;" or "exit(0);" was too firmly entrenched --
and it would have required nearly *every* program to have a
"#include <stdlib.h>".
 
E

Eric Sosman

Keith Thompson wrote On 01/19/06 16:14,:
And since 0 is required to indicate success, there's probably no good
reason for an implementation to define EXIT_SUCCESS as anything other
than 0 -- but it could, and you shouldn't assume that EXIT_SUCCESS==0.
(There's little reason to make such an assumption anyway.)

The very O/S you proceed to cite offers a good reason
for EXIT_SUCCESS != 0 ...
IMHO, the standard would have been clearer and more consistent if it
had *not* required 0 to indicate success, just defining EXIT_SUCCESS
and EXIT_FAILURE with implementation-specific values. The requirement
causes some problems for VMS^H^H^H OpenVMS, where odd values denote
success and even values denote errors; the C runtime system has to
make special allowances for "return 0;".

In the Very Old Days, a C program's exit status was
passed back verbatim as a VMS condition code, so exit(0)
meant "failure" ("warning," actually -- there were five
severity levels) and exit(1) meant "success." At some
point DEC decided to bow to prevailing custom, and put
in a hack that mapped 0->1 and 1->0 but left other values
untouched. That way, exit(0) and exit(1) had the meanings
on VMS that were not too different from what people were
accustomed to on Unix, while VMS-aware programs could still
exit with more informative VMS codes.

Then the Standard came along, and we had EXIT_SUCCESS
and EXIT_FAILURE. If I recall correctly (it's been a few
years), DEC didn't define EXIT_SUCCESS as 0 and let the
library change it to 1, but defined it as a large odd
number, turning "generic success" into "success of C program."
Similarly, EXIT_FAILURE wasn't defined as 1 to be mapped to
0, but as a large even value meaning "failure of C program."
Sounds like a silly distinction when viewed in isolation,
but remember: VMS condition codes were structured, with
various fields conveying different pieces of information.
By picking apart the fields you could determine not only
success/failure, but a severity level (five were defined),
a specific condition (a program could succeed in many ways,
or fail in many ways), and the system component where the
condition arose. A bare zero or one omitted most of this
information, whereas EXIT_xxxx supplied it.
 
K

Keith Thompson

Eric Sosman said:
Keith Thompson wrote On 01/19/06 16:14,:

The very O/S you proceed to cite offers a good reason
for EXIT_SUCCESS != 0 ...

Given that exit(0) is required to denote success, defining
EXIT_SUCCESS as 0 would be perfectly sensible even under VMS.
Defining it as some arbitary odd value could have some advantages,
though. For one thing, it might be somehow useful for exit(0) and
exit(EXIT_SUCCESS) to behave in some subtly different manner.

In fact, I just remembered I have an account on a VAX running OpenVMS
6.2. A quick test shows that EXIT_SUCCESS is defined as 0, and
EXIT_FAILURE is defined as 268435458 (0x10000002).
In the Very Old Days, a C program's exit status was
passed back verbatim as a VMS condition code, so exit(0)
meant "failure" ("warning," actually -- there were five
severity levels) and exit(1) meant "success." At some
point DEC decided to bow to prevailing custom, and put
in a hack that mapped 0->1 and 1->0 but left other values
untouched. That way, exit(0) and exit(1) had the meanings
on VMS that were not too different from what people were
accustomed to on Unix, while VMS-aware programs could still
exit with more informative VMS codes.

Actually, if I recall correctly, it maps exit(0) to an odd value, but
it doesn't map exit(1). Back when I worked on VMS systems, I saw
programs that used exit(1) with the intent of indicating failure; they
didn't.
 

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,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top