So basically, I was wondering why I've seen many tutorials teach "return 0;" as a way to end a program, yet when I replace "return 1;" down below with "0;" nothing happens. I've only been able to get the program to pop up using "return 1;" so, if you can, please explain the difference, and what both means, or if this is bad practice in general. Thanks in advance! :)
1 2 3 4 5 6 7
#include <iostream>
int main ()
{
std::cout << "Hello, world!";
return 1;
}
Whatever you return from main typically gets fed back to whatever launched the program. A return value of zero indicates the program ran successfully, whereas nonzero indicates a failure of some kind.
For stand-alone programs it doesn't really matter all that much, as this code is often ignored. But for launching programs inside batch scripts, where multiple programs need to run in succession... an error code could halt the entire script.
And that's where my confusion starts. I knew that "0" meant all is well, and "non-0" meant some sort of problem. However, with "return 0;" or simply no "return" anything there wouldn't be a "Hello, world!" cmd popup. Only by telling it there was an error would the program run correctly, which is not an error. Maybe I don't know enough about C++ to make those judgements, but I think that this is still strange.
0 just tells the handler of the program(ie: OS) that it ENDED successfully
any other values would tell that something else did happen during program termination.
However, with "return 0;" or simply no "return" anything there wouldn't be a "Hello, world!" cmd popup
I'm guessing you are running it in its standalone(like exe for windows) form? It may be that you can't see the output bec. the termination was so fast.How about you try adding (say std::cin.get();) to wait for the any input before terminating the program.
To understand why program exit codes are useful, you kind of have to work in a command prompt.
Imagine I write a batch script that does a few steps:
- Back up a file (make a copy)
- Run the copy through a custom command-prompt program I made that changes it somehow (like an encryption or something)
- Upload that file to some site on the net.
Each of these steps are handled by 3 independent programs. But each step in the overall logic is dependent on the previous steps. If any one of the steps fail, I would want to stop the entire script. For example, it wouldn't make sense to upload the file in step 3 if step 2 failed to modify the file. Nor would it make sense to modify the file in step 2 if step 1 failed to create the file.
But my custom program knows nothing about steps 1 or 3. It just opens a file and modifies it. It has no idea the file is a copy or where it's going. Likewise, the other steps know nothing about my custom program. So how can they communicate with each other as to whether or not they did their part in the job successfully?
The answer is the exit code. If step 1 fails to copy the file... it will report that failure by returning a nonzero value from main -- telling my batch script that it failed. My script can then abort and possibly spit an error message to the user. Likewise, if my custom program is unable to make its modifications to the file for whatever reason, I can report that back to the script as well.