LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Rep: Reputation: 66
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: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,973
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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: 42
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: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Original Poster
Rep: Reputation: 66
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: 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.
 
  


Reply


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 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


All times are GMT -5. The time now is 06:36 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration