View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-21-2005, 04:35 PM   #1
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,531

Rep: Reputation: 74
return codes in c

Ok im just begining c programming and i got a question.

Whats with return codes in main?

i understand that when you return something to the calling program like in a custom function you can return values

but what is the use for return 1 if something in main failes.

nothing ever reads main's return code. Is it a usefull thing to do or is it just used for clarity in C programming, or perhaps it was used at some time by a now extint os ?

Last edited by exvor; 01-21-2005 at 04:38 PM.
Old 01-21-2005, 04:41 PM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,047
Blog Entries: 11

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
That's so the OS can use it in a script, for instance.

Old 01-21-2005, 04:45 PM   #3
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
To elaborate:
16:42 aluser@alf:~/C$ cat > test.c
int main() {
  return 42;
16:43 aluser@alf:~/C$ gcc -o test test.c
16:43 aluser@alf:~/C$ ./test
16:43 aluser@alf:~/C$ echo $?
If you want the really gory details, a process gets information about one of its children's exit status via wait() or waitpid(). Type man 2 wait for how to use those syscalls.
Old 01-21-2005, 04:49 PM   #4
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,531

Original Poster
Rep: Reputation: 74
Yea I could see how that would be benifical in linux in a shell script. You could test the value of the program to see if it was sucessfull or not.

I guess in windows it would be useless unless you were in effect using it in the same manner in a batch file. But a standalone program would have no use for it.

In my programing im trying not to side with either camp (windows / linux) i really started out learning just for linux but if i ever want to use this skill in a company enviorment Im gonna have to learn how to program in C for windows.

:P in either effect it should be interesting. I sure get a chukkle every time i crash CMD in windows xp when linux just give me a nice simple error "segmentaion fault"
Old 01-21-2005, 09:45 PM   #5
Registered: Jan 2005
Location: MA
Distribution: Ubuntu 7.10
Posts: 558

Rep: Reputation: 30
I am not that familiar with C but it is similar to java in many ways. If you really have aproblem with returning 1 then you can make main return void.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
PPP library & pppd, chat return codes ruchika Linux - Networking 1 03-08-2011 03:50 AM
Are the hex codes for colors in a jpg the same codes as used in html? abefroman Linux - Security 3 07-31-2005 04:21 PM
mprotect() return codes tim_l Programming 2 05-09-2005 08:38 AM
wht do these codes mean? jackandking Programming 10 12-25-2004 12:14 PM
GNU wget return codes for shell script greenhornet Programming 3 05-09-2004 08:51 PM

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

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