Return 0;

I understand that return 0 is used to show that the program completed successfully, but when I compile and run any program like a hello world program with it the 0 isn't displayed anywhere, so how is it actually suppose to tell me that the program ran successfully?

1
2
3
4
int main()
{
   return 0;
}
After running a program in an IDE, Look at the output box. It'll tell you what exit code main returned
Maybe it's just the IDE I'm using then because I see it in the output box on the video I'm currently watching, but in mine it just shows:

NPP_EXEC: "Execute Compiled File"
"C:\C++\Hello World"
Process started >>>
<<< Process finished.
================ READY ================

That value is returned to the calling process, which is usually your OS shell.

It typically doesn't mean anything to you. If you are making a program that can be used in shell programs you can indicate the success or failure of your program's function by returning 0 (success) or 1 (failure).

For example, the 'mkdir' shell program will try to make a subdirectory. If it succeeds, its exit code is 0. If the directory already exists (or it fails for any other reason), then its exit code is 1.

That way you can write a shell script that uses the 'mkdir' command and also find out whether it worked or not.


You can think of it this way: main() is a special function which gets called by whatever process executes your program. Returning zero is just a way to say, "all done, all is well."

This is so common that the C++11 standard does not require it explicitly. You can just write:

1
2
3
int main()
{
}

and the return 0; will be inserted automatically by the compiler.

Hope this helps.
closed account (E0p9LyTq)
Maybe it's just the IDE I'm using then because I see it in the output box on the video I'm currently watching, but in mine it just shows:

What is your IDE? In my IDE (Orwell's fork of Dev-C++) I see this:

Hello World

--------------------------------
Process exited after 0.0274 seconds with return value 0
Press any key to continue . . .
I'm using pocket cpp which is a built in compiler for notepad++ which is my IDE
Last edited on
The shell stores the value returned by a process into a shell environment variable:
http://stackoverflow.com/questions/334879/how-do-i-get-the-application-exit-code-from-a-windows-command-line

Interactive CLI user (or a script) can thus inspect the value.

An IDE uses system functions to execute the compiled program. Those functions return (one way or other) the returnvalue and the IDE can store it, inspect it, and show it.


/rant
Certain program (not IDE) did have a (GUI) dialog with a helpful message: "These changes will be irreversible and can render your computer unusable." The dialog had also a button. One button with that friendly text: "OK". No chance for cancel or second thoughts.

We have seen on this Forum many "applications" (neither shell nor IDE) that execute programs, but how many times does such application use the return value of system()?
Topic archived. No new replies allowed.