Old 04-10-2008, 02:51 AM   #1
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default Why is MAIN of type INT ?

Seriously OT I know, but...

Why is this?
__________________
REAPER? Oh yes...
Deric is offline   Reply With Quote
Old 04-10-2008, 03:13 AM   #2
404NotFound
Human being with feelings
 
404NotFound's Avatar
 
Join Date: Oct 2007
Location: Berlin, GER
Posts: 199
Default

the number the main() function returns is your program's error code. It lets other applications/the shell know if your program was executed successfully.
404NotFound is offline   Reply With Quote
Old 04-10-2008, 03:23 AM   #3
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default

404NotFound,

Thanks for the quick response

Many thanks for answering this - I'm getting my head around the how/what/why of C++ (since the end of last week) and this is very useful.

Edit : I'd like to know just this now:

How can another program query what the output (of the initial main() ) is?

Edit: Following questions answered after more reading (apologies if anyone's started typing a response!)

Does that mean that return 0; gives main() a value of zero?
Edit: Yes - if the Return is within the main() routine.

If this is the case, does void exit (int n); supply main() with n?
Edit: Yes.

Answers to any of the above much appreciated!

Cheers
__________________
REAPER? Oh yes...

Last edited by Deric; 04-10-2008 at 05:13 AM. Reason: Answered 2of3 questions.
Deric is offline   Reply With Quote
Old 04-10-2008, 05:37 AM   #4
404NotFound
Human being with feelings
 
404NotFound's Avatar
 
Join Date: Oct 2007
Location: Berlin, GER
Posts: 199
Default

I just spent over an hour looking for an answer... it looks like it's rather difficult to query exit codes from within a program. I haven't found any simple solution yet.

Exit codes are usually used by shell scripts (like Windows .bat or .cmd files and Unix .sh scripts). They can use them for simple case switching, you can also pass the code to another program as an argument.
404NotFound is offline   Reply With Quote
Old 04-10-2008, 07:39 AM   #5
sws
Code Monkey
 
sws's Avatar
 
Join Date: Sep 2007
Location: Madison, WI
Posts: 851
Default

If you execute a subprogram from within your code using _spawnl() or its derivates it will return the exit code of the subprogram. The way I understand it, the only process that can see the exit code is the parent process (often explorer.exe in Windows or your shell in unix).
sws is offline   Reply With Quote
Old 04-10-2008, 09:38 AM   #6
Deric
Human being with feelings
 
Join Date: Mar 2007
Posts: 794
Default

Thanks to both of you.

404NotFound - that is extremely kind of you, I hope my progress (in this area) will be of use to you too soon, thank you.

OK, so far we agree (I think) that we can return a code to a function but we don't know how to determine that functions returned code from another program.

There should be a way to define this (return code) as some sort of 'program variable' (sorry - my terminology is currently lacking) such that, should another program run the program, it can determine it's return code (if that code !=0).

Thanks for all the generous help so far! (I love this place)
__________________
REAPER? Oh yes...
Deric is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 04:27 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.