The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
what is meaning of exit(0) and exit(1) amitpansuria UNIX for Dummies Questions & Answers 1 08-28-2007 12:02 AM
exit 2 xramm Shell Programming and Scripting 2 08-15-2007 06:48 AM
exit dhananjaysk Shell Programming and Scripting 2 04-06-2006 05:34 AM
Where can I find a list of exit codes? (Exit code 64) jkuchar747 UNIX for Dummies Questions & Answers 3 12-07-2004 03:08 PM
How to exit ssh davidg UNIX for Advanced & Expert Users 1 10-16-2003 04:10 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-01-2007
Registered User
 

Join Date: Mar 2007
Posts: 39
exit(0) versus exit(1)

What is the difference between using exit(0) and exit(1) to exit a program? Which should I use?
Reply With Quote
Forum Sponsor
  #2  
Old 06-01-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,663
You normally use exit(0) if everything went ok. But if your program detects an error and decides to abort, you would use exit(1). Or you might use exit(1), exit(2), etc, with each exit code meaning some specific error.
Reply With Quote
  #3  
Old 06-01-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
For normal exits use 0 (recommended)
and for abnormal program termination or incomplete ones use 1 (recommended)


these exit status are indication to the environment from which the program has run about the status of exit of the program
Reply With Quote
  #4  
Old 06-01-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
What to do you want to tell your parent?

The rules is generally if all went went and you did what you were told (as all children should) then return 0.

If something went wrong then return non-zero, preferably between 1 and 127.

This is termed the exit code of a process and in a shell becomes the "$?" value.

This allows you to do the following

Code:
if myprogram
then 
      echo all went well
else 
      echo bad things
fi
Reply With Quote
  #5  
Old 06-01-2007
Registered User
 

Join Date: Mar 2007
Posts: 39
Thanks for the tips.
Reply With Quote
  #6  
Old 06-16-2007
Registered User
 

Join Date: May 2006
Posts: 101
hi enuenu,
actualy you should use


#include <stdlib.h>

exit(EXIT_SUCCESS);
exit(EXIT_FAILURE);


just my 2 cents
Reply With Quote
  #7  
Old 06-17-2007
Registered User
 

Join Date: Mar 2007
Posts: 39
Thanks, I am still investigating (this and much more).
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0