Difference between exit(0) & exit (1)

P

Parahat Melayev

The C standard specifies two defines EXIT_SUCCESS and EXIT_FAILURE that
may be passed to exit() to indicate successful or unsuccessful
termination, respectively.

The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to
non-Unix environments) than that of 0 and some nonzero value like 1 or
-1. In particular, VMS uses a different convention.

more can be found at "man 3 exit"...
 
K

Kenneth Brody

Please include the question in the body of your message.

The question (sort of), as stated in the subject is:

Difference between exit(0) & exit (1)
Please tell me at
(e-mail address removed)

I will post the answer here, and not by e-mail, as that's the purpose
of Usenet.

exit(0)

This causes the program to exit with a successful termination.

exit(1)

This causes the program to exit with a system-specific meaning.

On many systems, exit(1) signals some sort of failure, however there
is no guarantee.

As I recall, the C standard only recognizes three standard exit
values:

EXIT_SUCCESS -- successful termination
EXIT_FAILURE -- unsuccessful termination
0 -- same as EXIT_SUCCESS

Now, as to what happens after the program exits, is beyond the scope
of the C standard.

--
+-------------------------+--------------------+-----------------------+
| 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

Michael Brennan

Parahat said:
The C standard specifies two defines EXIT_SUCCESS and EXIT_FAILURE that
may be passed to exit() to indicate successful or unsuccessful
termination, respectively.

It also says 0 is a valid return value to indicate success, so it
is as portable as EXIT_SUCCESS is. Any other value than these three
will be an implementation-defined status.
 
K

Keith Thompson

Parahat Melayev said:
The C standard specifies two defines EXIT_SUCCESS and EXIT_FAILURE that
may be passed to exit() to indicate successful or unsuccessful
termination, respectively.

The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to
non-Unix environments) than that of 0 and some nonzero value like 1 or
-1. In particular, VMS uses a different convention.

I would drop the word "slightly".

exit(0) is just as portable as exit(EXIT_SUCCESS); they're both
defined by the C standard to indicate successful termination.

exit(1) is non-portable, and I've seen plenty of code that uses it
incorrectly. exit(EXIT_FAILURE) (or "return EXIT_FAILURE;" within
main()) is the only portable way to indicate unsuccessful termination.
more can be found at "man 3 exit"...

Only if your system has the "man" command, and if it does it's likely
to give you system-specific information. Of course there's nothing
wrong with learning how things work on your system, but it's also
important to know what's portable and what isn't, and a
system-specific man page may or may not tell you that.
 
K

Keith Thompson

Kenneth Brody said:
On many systems, exit(1) signals some sort of failure, however there
is no guarantee.
Right.

As I recall, the C standard only recognizes three standard exit
values:

EXIT_SUCCESS -- successful termination
EXIT_FAILURE -- unsuccessful termination
0 -- same as EXIT_SUCCESS

Both 0 and EXIT_SUCCESS indicate successful termination, but there's
no guarantee that EXIT_SUCCESS == 0; EXIT_SUCCESS could be some
non-zero value that also indicates successful termination.

On the other hand, there's no good reason for an implementation to
define EXIT_SUCCESS as anything other than 0, and I've never heard of
an implementation that does so (not even VMS). Portable code wouldn't
be able to make use of the distinction.
 
K

Kenneth Brody

Keith said:
Both 0 and EXIT_SUCCESS indicate successful termination, but there's
no guarantee that EXIT_SUCCESS == 0; EXIT_SUCCESS could be some
non-zero value that also indicates successful termination.

Well, I probably should have been clearer. I didn't mean that
EXIT_SUCCESS had to be defined as zero. Rather, I meant that
exit(0) meant the same as exit(EXIT_SUCCESS). However, if the
standard allows both zero and EXIT_SUCCESS to mean two different
types of "success", then so be it.
On the other hand, there's no good reason for an implementation to
define EXIT_SUCCESS as anything other than 0, and I've never heard of
an implementation that does so (not even VMS). Portable code wouldn't
be able to make use of the distinction.

True, even if the standard allows zero and EXIT_SUCCESS to be
distinct types of "success", portable code can't depend on being
able to distinguish between the two.

--
+-------------------------+--------------------+-----------------------+
| 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]>
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top