Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-06-2005, 09:27 AM
|
#1
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203
Rep:
|
How do we use exit()
Hi... i have just read about the exit() and the error messages that we pass to the exit as arguments.. I am very curious to learn what is the difference between an exit (1) and an exit(2) command... and that's why because i the exit doesnot return a value... so what;s the meaning feeding exit with arguments.. Any ideas?
Thx a lot
|
|
|
06-06-2005, 09:50 AM
|
#2
|
LQ Guru
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,381
|
"Hi... i have just read about the exit() and the error messages that we pass to the exit as arguments.. I am very curious to learn what is the difference between an exit (1) and an exit(2) command... and that's why because i the exit doesnot return a value... so what;s the meaning feeding exit with arguments.. Any ideas?"
The exit argument is the return code that the program returns to the calling program. Typically people use a zero return code to mean errror free completion. Positive integers usually mean errors of various kinds. It is up to the programmer to describe what each error code signifies.
-------------------------
Steve Stites
|
|
|
06-06-2005, 09:53 AM
|
#3
|
Member
Registered: Jan 2005
Location: Innsbruck, Austria
Distribution: Debian GNU/Linux Lenny
Posts: 68
Rep:
|
Write a little program which does an exit(0). Compile it and run.
$ ./a.out
$ echo $?
0
Then, do the same thing with exit(0).
$ ./a.out
$ echo $?
1
The value to exit() is the program's exit code. You should exit(0) on success and exit(> 0) on failure. This is to allow the calling application to check if the called program succeeded. One of the easiest examples is a shell script which does
if ./a.out
then
echo Success!
else
echo Fail...
fi
to see if there were any problems.
|
|
|
06-07-2005, 02:58 AM
|
#4
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203
Original Poster
Rep:
|
and if i want to return an error code from a function then u should use the return right?
|
|
|
06-07-2005, 09:44 AM
|
#5
|
Member
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340
Rep:
|
Quote:
Originally posted by alaios
and if i want to return an error code from a function then u should use the return right?
|
In C and most languages, yes. In C/C++ make sure you return an int (or a short int or char, etc...)
|
|
|
All times are GMT -5. The time now is 12:18 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|