Return value of main

M

Morris Dovey

polas said:
Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?

If you include <stdlib.h>, the symbols EXIT_SUCCESS and
EXIT_FAILURE will be defined appropriately.
 
P

polas

Good afternoon all,

I appreciate that the standard requires that main should return an
integer. What I was wondering though is there any accepted
standardisation on exactly what value to return - do people tend to
return 0 or 1 or anything else? In addition, if there has been some
failure, do people tend to return another value (such as -1?)

Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?

Cheers,
Nick
 
R

Richard Bos

polas said:
I appreciate that the standard requires that main should return an
integer. What I was wondering though is there any accepted
standardisation on exactly what value to return - do people tend to
return 0 or 1 or anything else?

The Standard requires three values to return a definite status to the
environment: 0 and EXIT_SUCCESS signal success, EXIT_FAILURE signals
failure. EXIT_SUCCESS and EXIT_FAILURE are macros #defined in
<stdlib.h>; what their actual values are is not defined by the Standard,
and may vary from implementation to implementation. Of course it is
highly common for EXIT_SUCCESS to be 0, and EXIT_FAILURE is often 1, but
neither of those is guaranteed.
If you return any other value from main() (or through exit()), this does
not make the program exhibit undefined behaviour; the only result is
that the meaning of the status returned to the environment is undefined
by the Standard. So calling exit(2) does not make your program print
weird things or write over memory it doesn't own; but it does mean that
the C Standard itself does not guarantee that other programs will see
this as your program having succeeded, failed, or otherwise.
Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?

That depends entirely on the OS.
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?

It does; hence the existence of EXIT_SUCCESS and EXIT_FAILURE, which can
be adapted to the OS' needs.

Richard
 
P

polas

If you include <stdlib.h>, the symbols EXIT_SUCCESS and
EXIT_FAILURE will be defined appropriately.

Thanks for the replies - that clears it up completely for me.

Nick
 
S

santosh

polas said:
Good afternoon all,

I appreciate that the standard requires that main should return an
integer. What I was wondering though is there any accepted
standardisation on exactly what value to return - do people tend to
return 0 or 1 or anything else? In addition, if there has been some
failure, do people tend to return another value (such as -1?)

The only standard values for these are 0, EXIT_SUCCESS and EXIT_FAILURE.
Zero is the same as EXIT_SUCCESS. For using any other values (like
1, -1 etc.) you'll have to seek the guarantees of standards beyond ISO
C.
Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?

No such standard behaviour is specified by ISO C. Each system might have
it's own standardised or semi-standard behaviour. The portability of C
code in your example will not be affected because a return of zero
always means successful termination and the C library is obliged to
translate this to whatever code that the system uses for successful
termination. Thus if for the underlying system 10 indicates successful
termination and you return zero, the C library will actually return 10.
It will do the same for a return of EXIT_SUCCESS too. A recompile for
each platform will ensure correct behaviour.
 
P

polas

The only standard values for these are 0, EXIT_SUCCESS and EXIT_FAILURE.
Zero is the same as EXIT_SUCCESS. For using any other values (like
1, -1 etc.) you'll have to seek the guarantees of standards beyond ISO
C.


No such standard behaviour is specified by ISO C. Each system might have
it's own standardised or semi-standard behaviour. The portability of C
code in your example will not be affected because a return of zero
always means successful termination and the C library is obliged to
translate this to whatever code that the system uses for successful
termination. Thus if for the underlying system 10 indicates successful
termination and you return zero, the C library will actually return 10.
It will do the same for a return of EXIT_SUCCESS too. A recompile for
each platform will ensure correct behaviour.

Right - just to clear up a minor point, is 0 defined by the standard
to be success (as well as EXIT_SUCCESS)?

Nick
 
R

Richard Bos

polas said:
Right - just to clear up a minor point, is 0 defined by the standard
to be success (as well as EXIT_SUCCESS)?

Yes. (As an aside, it's not _guaranteed_ that 0 and EXIT_SUCCESS are the
same kind of success, but in practice, I don't think there are many
ssytems which have more than one kind anyway.)

Richard
 
J

Jean-Marc Bourguet

Yes. (As an aside, it's not _guaranteed_ that 0 and EXIT_SUCCESS are the
same kind of success, but in practice, I don't think there are many
ssytems which have more than one kind anyway.)

IIRC, VMS has several kinds of success: every even result is a failure and
every odd one a success; the runtime does something particular in the case
of 0 so that it has its standard behaviour instead of the VMS one).

Yours,
 
K

Kenneth Brody

Richard said:
The Standard requires three values to return a definite status to the
environment: 0 and EXIT_SUCCESS signal success, EXIT_FAILURE signals
failure. EXIT_SUCCESS and EXIT_FAILURE are macros #defined in
<stdlib.h>; what their actual values are is not defined by the Standard,
and may vary from implementation to implementation. Of course it is
highly common for EXIT_SUCCESS to be 0, and EXIT_FAILURE is often 1, but
neither of those is guaranteed.

Well, it is guaranteed that EXIT_FAILURE cannot be defined as zero.
It does; hence the existence of EXIT_SUCCESS and EXIT_FAILURE, which can
be adapted to the OS' needs.

Note that exit(0) will still signal "success", even in this case.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

Martin Ambuhl

polas said:
Good afternoon all,

I appreciate that the standard requires that main should return an
integer. What I was wondering though is there any accepted
standardisation on exactly what value to return - do people tend to
return 0 or 1 or anything else? In addition, if there has been some
failure, do people tend to return another value (such as -1?)

Without including <stdlib.h>, there is only one portable value, 0, which
indicates successful completion.

If you include <stdlib.h>, you have available the values EXIT_SUCCESS
and EXIT_FAILURE.

Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?

There were C implementations that when the C program returned 0 would
indicate to the operating system that the program had failed. Such
implementations, as far as I know, are no longer used and obsolete. If
you want to indicate successful completion, return 0 or EXIT_SUCCESS.
If you want to portably indicate failure, return EXIT_FAILURE. Your
implementation and operating system might define meanings for other
values, but they are not portable or defined by the C language.
 
R

Richard Tobin

Martin Ambuhl said:
Without including <stdlib.h>, there is only one portable value, 0, which
indicates successful completion.

And of course the C standard can't really guarantee anything much
about this "success".

For example, on Unix, the return code is not interpreted by the
operating system as such, but by the parent program. Even if you run
your C program from the shell, the exit value is only treated as
"success" or "failure" in a very few circumstances. More often it is
treated as a boolean value, and even more often it is not interpreted
at all.

-- Richard
 
U

user923005

The only standard values for these are 0, EXIT_SUCCESS and EXIT_FAILURE.
Zero is the same as EXIT_SUCCESS. For using any other values (like
1, -1 etc.) you'll have to seek the guarantees of standards beyond ISO
C.


No such standard behaviour is specified by ISO C. Each system might have
it's own standardised or semi-standard behaviour. The portability of C
code in your example will not be affected because a return of zero
always means successful termination and the C library is obliged to
translate this to whatever code that the system uses for successful
termination. Thus if for the underlying system 10 indicates successful
termination and you return zero, the C library will actually return 10.
It will do the same for a return of EXIT_SUCCESS too. A recompile for
each platform will ensure correct behaviour.

From the standard ISO/IEC 9899:1999 (E) we have this:
"5.1.2.2.3 Program termination
1 If the return type of the main function is a type compatible with
int, a return from the initial call to the main function is equivalent
to calling the exit function with the value returned by the main
function as its argument;10) reaching the } that terminates the main
function returns a value of 0. If the return type is not compatible
with int, the termination status returned to the host environment is
unspecified.
Forward references: definition of terms (7.1.1), the exit function
(7.20.4.3)."

And this:
"7.20.4.3 The exit function
Synopsis
1 #include <stdlib.h>
void exit(int status);
Description
2 The exit function causes normal program termination to occur. If
more than one call to the exit function is executed by a program, the
behavior is undefined.
3 First, all functions registered by the atexit function are called,
in the reverse order of their registration,253) except that a function
is called after any previously registered functions that had already
been called at the time it was registered. If, during the call to any
such function, a call to the longjmp function is made that would
terminate the call to the registered function, the behavior is
undefined.
4 Next, all open streams with unwritten buffered data are flushed, all
open streams are closed, and all files created by the tmpfile function
are removed.
5 Finally, control is returned to the host environment. If the value
of status is zero or EXIT_SUCCESS, an implementation-defined form of
the status successful termination is returned. If the value of status
is EXIT_FAILURE, an implementation-defined form of the status
unsuccessful termination is returned. Otherwise the status returned is
implementation-defined.
Returns
6 The exit function cannot return to its caller."

So we can conclude that return 0 or return EXIT_SUCCESS will return
successful status and that return EXIT_FAILURE will return
unsuccessful status and that any other value must be implementation
defined.
 
P

pete

Kenneth Brody wrote:
Well, it is guaranteed that EXIT_FAILURE cannot be defined as zero.

That's not guaranteed.

Some systems don't make use of the return value of main,
and on systems that don't,
there is no reason to have any meaning at all
encoded into the values that main can return.
 
G

Gordon Burditt

So we can conclude that return 0 or return EXIT_SUCCESS will return
successful status and that return EXIT_FAILURE will return
unsuccessful status and that any other value must be implementation
defined.

Nothing in that statement prohibits different types of success or
failure within the broad classes, such as:

EXIT_SUCCESS_WITH_PROMOTION_AND_RAISE
EXIT_SUCCESS_YOU_GET_TO_KEEP_YOUR_JOB
EXIT_FAILURE_YOURE_FIRED
EXIT_FAILURE_YOU_WILL_BE_SHOT

Although it's not allowed for <stdlib.h> to define these, they could
be defined in an implementation-specific header.
 
S

santosh

So we can conclude that return 0 or return EXIT_SUCCESS will return
successful status and that return EXIT_FAILURE will return
unsuccessful status and that any other value must be implementation
defined.

That's what I believe I said too.
 

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

Similar Threads

return value of main 25
main return codes 17
Linux: using "clone3" and "waitid" 0
ANN main-4.4.0 0
fscanf() return value 6
[ANN] main-3.0.1 0
Can main() return any int value? 9
main 7

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top