Haoran Shan
last year
Haorancountry asked

What is the use of return 0;?

Kelish Rai
Expert
last year
Kelish Rai answered

In C++, the return 0; statement simply indicates that the program has completed successfully. It tells the system that the program ran without errors.

For example:

#include 
using namespace std;

int main() {
    cout << "Hello, world!" << endl;
    return 0;
}

In the code:

  • The program prints "Hello, world!".

  • The return 0; line sends a status code back to the system—0 traditionally means success.

  • If something went wrong in your program and you wanted to signal that to the system, you could return a non-zero value like return 1;.

Why does this matter?

If you're running your program in a larger system or calling it from a script, the return value lets that system know whether the program succeeded. This is useful in automated testing, system monitoring, or when chaining commands in shell scripts.

So while it may seem small, return 0; plays an important role in communicating the outcome of your program to the system.

C++
This question was asked as part of the Learn C++ Basics course.