LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-21-2005, 03:35 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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 03:38 PM.
 
Old 01-21-2005, 03:41 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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


Cheers,
Tink
 
Old 01-21-2005, 03:45 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
To elaborate:
Code:
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 $?
42
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, 03:49 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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, 08:45 PM   #5
tamoneya
Member
 
Registered: Jan 2005
Location: MA
Distribution: Ubuntu 7.10
Posts: 558

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


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
PPP library & pppd, chat return codes ruchika Linux - Networking 1 03-08-2011 02: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 03:21 PM
mprotect() return codes tim_l Programming 2 05-09-2005 07:38 AM
wht do these codes mean? jackandking Programming 10 12-25-2004 11:14 AM
GNU wget return codes for shell script greenhornet Programming 3 05-09-2004 07:51 PM

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

All times are GMT -5. The time now is 01:31 PM.

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