LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-06-2005, 09:27 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
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
 
Old 06-06-2005, 09:50 AM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
"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
 
Old 06-06-2005, 09:53 AM   #3
mschutte
Member
 
Registered: Jan 2005
Location: Innsbruck, Austria
Distribution: Debian GNU/Linux Lenny
Posts: 68

Rep: Reputation: 15
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.
 
Old 06-07-2005, 02:58 AM   #4
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
and if i want to return an error code from a function then u should use the return right?
 
Old 06-07-2005, 09:44 AM   #5
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
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...)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to exit vi? lommer0 Linux - General 22 12-11-2018 02:04 AM
difference between exit 1 and exit kushalkoolwal Programming 6 10-20-2005 03:56 PM
exit X ? w00t Linux - Newbie 11 06-01-2003 08:04 PM
exit xlord Programming 7 11-03-2002 11:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration