WIPIVERSE All articles

return 0

return 0 is a statement in the C and C++ programming languages (and many other C-family languages) used within the main() function to indicate successful program termination to the operating system. It is the conventional way of signaling that a program completed without errors.

Overview

In a hosted C or C++ environment, the main() function is declared to return an int (integer) value. The value returned by main() becomes the program's exit status or exit code, which is passed back to the operating system (or the calling process) after the program terminates. By convention:

  • A return value of 0 (zero) indicates successful execution.
  • A return value of non-zero indicates that an error or exceptional condition occurred.

This convention is defined in the C and C++ language standards and is widely adopted across Unix, Linux, Windows, and other operating systems.

Technical Details

When main() executes a return 0; statement, the following occurs:

  1. The function main() terminates and returns the integer value 0.
  2. The C runtime library (typically via a function like exit()) receives this value.
  3. The operating system kernel records the exit status, making it available to the parent process (e.g., a shell script or command-line interpreter).
  4. Any functions registered with atexit() are called, and global/static object destructors in C++ are executed before the process fully terminates.

In C99 and later, reaching the closing brace of main() without an explicit return statement is equivalent to return 0; (the compiler implicitly returns 0). However, explicitly writing return 0; is still considered good practice for clarity.

Historical Context

The convention of using zero for success originates from earlier Unix systems. In Unix, a process exit status of 0 means "success," while any non-zero value (1–255) means "failure" or a specific error condition. This pattern was carried into the C language design and subsequently into C++, Java (System.exit(0)), Python (sys.exit(0)), and many other languages.

The C standard library defines the macros EXIT_SUCCESS (typically 0) and EXIT_FAILURE (typically 1) in <stdlib.h> as portable alternatives for indicating success or failure.

Usage Example

#include <stdio.h>

int main() {
    printf("Hello, world!
");
    return 0;   // Signal that the program completed successfully
}

Comparison with return 1

Return Value Meaning
return 0; Success (program ran without errors)
return 1; General error or failure
Other values Custom error codes (implementation-defined)

The specific meaning of non-zero exit codes is generally platform-defined; the C standard only defines EXIT_SUCCESS (0) and EXIT_FAILURE (1 on most systems).

Significance in Shell Scripting

In shell environments (e.g., Bash), the exit code of the last executed command is stored in the special variable $?. A value of 0 tests as "true" in conditional expressions, while non-zero tests as "false." This allows scripts to check program success:

./myprogram
if [ $? -eq 0 ]; then
    echo "Program succeeded"
else
    echo "Program failed"
fi

Relation to the exit() Function

Beyond return 0;, a program can also terminate with the same exit status by calling exit(0); from anywhere in the code. The exit() function does not return; it performs cleanup operations and then terminates the process with the given status code.

Browse

More topics to explore

    Browse all articles