Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
int main()
{
    .......
    return 0;
}


As we know, return 0; is used for terminating a program. But where is the value returns to ??
Please help me with this silly question. Thank you =]
Posted
Comments
Legor 24-May-11 7:15am    
both answers below are right. please exaccept one (or more) of the solutions.
Marc A. Brown 24-May-11 14:21pm    
Legor is correct. All 3 answers are good. Please accept at least one of them, perhaps even all of them, as the correct (accepted) answer(s).

To the Operating System.
See, for instance, how can you see such return value on Windows (courtesy of 'the competitors'...):"how do I get the application exit code from a Windows command line[^] at StackOverflow.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-May-11 14:05pm    
Correct, but...
The answer is so simple, please see mine.
--SA
Marc A. Brown 24-May-11 14:20pm    
Good answer!
Read this[^]...

Those return values are used to know the exit status of an app... (i.e. you could call an app from yours and you could be interested on knowing how has it finished).

HTH! :thumbsup:
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-May-11 14:06pm    
Correct, good reference, my 5.
I simply pot short complete answer, please see.
--SA
Joan M 25-May-11 2:08am    
Thank you SAKryukov!
love_small 25-May-11 9:18am    
where i can see?
Joan M 25-May-11 9:23am    
I guess you wanted to originally post to SAKRyukov... and that at the beginning you've posted to me... Anyway, you can see his post a little bit forward in that same page. (SOLUTION 3). ;)
Marc A. Brown 24-May-11 14:20pm    
Great answer. My 5.
No! Return 0 is not used to terminate a program! Who told you so?

The return value is called exit code.

On Windows, exit code value can be examined by batch file or a code in parent process (via Windows API CreateProces or CreateProcessEx) after the application is closed.
(Thanks to C Pallini for pointing out that this is applicable to Windows only; I forgot to mention it.)

In other system (at least all Unix-like ones and other Posix-based which includes Windows), the exit code can also can be examined by the command interpreters and the parent processes in general. Traditionally, 0 is used to indicate "no problem", "success", but the exit code can mean anything at all, this is application-specific. In present-day practice, used rarely.

That's all you need to know about it.

—SA
 
Share this answer
 
v3
Comments
Albert Holguin 24-May-11 14:11pm    
as always, very informative... but as a batch builder, please let that mean something in your apps! :)
Sergey Alexandrovich Kryukov 24-May-11 14:17pm    
Thank you Albert.
I feel you want to say something interesting about a batch builder... but what exactly do you mean?
Practically, I think MSBuild can do everything in declarative manner and it the batch approach should be avoided. I learned to write highly customized MSBuild projects manually and know it has absolute power. You only need batch file to short-cut the call to MSBuild with parameters itself.
Albert Holguin 24-May-11 14:21pm    
I mean creating batch files to accomplish something outside of the app, for example, I have an automatic build script that checks if anyone has checked in crappy code onto the server... in a similar fashion, if I were using software you built and I needed to run it in conjunction with other software via some sort of batch that was controlling the process, it'd be nice to have feedback to make sure everything executed correctly without errors... my automatic build script will e-mail me if there's errors in the process, it sits in a server in a back room.
Sergey Alexandrovich Kryukov 24-May-11 15:40pm    
Yes, sure. Then again, I maintain it is not very much needed as MSBuild covers all the needs; and you can use/embed any other console applications or write a special MSBuild task assembly DLL.

That said, exit code can be used with MSBuild as well, as with any other applications capable of getting the exit code from a terminated child process.
--SA
Albert Holguin 24-May-11 15:51pm    
msbuild only relates to builds though, what about just coordinating processes...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900