Open In App

return 0 vs return 1 in C++

Last Updated : 20 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Return statement in C/C++:

There are two scenarios in which return statements will be used:                  

Method 1. Inside the main function:

  • In this case, the return statement stops the execution of the program, and 0 or 1 will denote the execution status.
  • These status codes will be just used as a convention for a long time in C language because the language does not support the objects and classes, and exceptions.
  • return 0: A return 0 means that the program will execute successfully and did what it was intended to do.
  • return 1: A return 1 means that there is some error while executing the program, and it is not performing what it was intended to do.

Important characteristics of the return statement: 

  • If exit with a status other than 0 then, print an error message to stderr.
  • There are different conventions depending on the operating system about return codes.
  • The Operating System may itself terminate the program with specific exit status codes if some invalid operations are performed.

Below is a program to illustrate the use of return 0 and return 1 inside the main function:

C++




// C++ program to divide two numbers
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Given integers
    int a = 5, b = 0;
 
    if (b == 0) {
 
        // The below line is used to print
        // the message in the error window
        // fprintf(stderr, "Division by zero"
        //                 " is not possible.");
 
        // Print the error message
        // as return is -1
        printf("Division by zero is"
               " not possible.");
        return -1;
    }
 
    // Else print the division of
    // two numbers
    cout << a / b << endl;
 
    return 0;
}


Output:

Division by zero is not possible.

Time Complexity: O(1)
Auxiliary Space: O(1)

Method 2. Inside the user-defined function:

  • C++ treats boolean as a completely separate data type that has only 2 distinct values, i.e., true and false.
  • The values 1 and 0 are of type int and are not implicitly convertible to boolean, that means:
    • return 0: returning false from a function.
    • return 1: returning true from a function.

Below is a program to illustrate the use of return 0 and return 1 inside the user-defined function:

C++




// C++ program to demonstrate the use
// of return 0 and return 1 inside
// user-defined function
#include <iostream>
using namespace std;
 
// Utility function returning 1 or
// 0 based on given age
int checkAdultUtil(int age)
{
    if (age >= 18)
        return 1;
    else
        return 0;
}
 
// Function to check for age
void checkAdult(int age)
{
    // Checking on the basis
    // of given age
    if (checkAdultUtil(age))
        cout << "You are an adult\n";
    else
        cout << "You are not an adult\n";
}
 
// Driver Code
int main()
{
    // Given age
    int age = 25;
 
    // Function Call
    checkAdult(age);
 
    return 0;
}


Output:

You are an adult

Time Complexity: O(1)
Auxiliary Space: O(1)

Conclusion:

Use-case return 0 return 1
In the main function return 0 in the main function means that the program executed successfully. return 1 in the main function means that the program does not execute successfully and there is some error.
In user-defined function return 0 means that the user-defined function is returning false. return 1 means that the user-defined function is returning true.


Similar Reads

Return Statement vs Exit() in main() in C++
The return statement in C++ is a keyword used to return the program control from the called function to the calling function. On the other hand, the exit() function in C is a standard library function of &lt;stdlib.h&gt; that is used to terminate the process explicitly. The operation of the two may look different but in the case of the main() funct
3 min read
How to Return a Local Array From a C++ Function?
Here, we will build a C++ program to return a local array from a function. And will come across the right way of returning an array from a function using 3 approaches i.e. Using Dynamically Allocated ArrayUsing Static Array Using Struct C/C++ Code // C++ Program to Return a Local // Array from a function While // violating some rules #include &lt;i
3 min read
How to pass and return a 3-Dimensional Array in C++?
In C++ a 3-dimensional array can be implemented in two ways: Using array (static)Using vector (dynamic) Passing a static 3D array in a function: Using pointers while passing the array. Converting it to the equivalent pointer type. char ch[2][2][2];void display(char (*ch)[2][2]) { . . .} Program to pass a static 3D array as a parameter: C/C++ Code /
3 min read
What does main() return in C and C++?
C According to coding standards, a good return program must exit the main function with 0. Although we are using void main() in C, In which we have not suppose to write any kind of return statement but that doesn't mean that C code doesn't require 0 as exit code. Let's see one example to clear our thinking about need of return 0 statement in our co
3 min read
return statement in C++ with Examples
Pre-requisite: Functions in C++ The return statement returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was called. The return statement ma
4 min read
How to pass or return a structure to/from a Function in C/C++?
A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. How to pass structure as an argument to the functions? Passing of structure to the function can be done in two ways: By passing all the elements to the function individually. By passing th
3 min read
C++ Return 2D Array From Function
An array is the collection of similar data-type stored in continuous memory. And when we are storing an array inside an array it is called 2 D array or 2-dimensional array. To know more about arrays refer to the article Array in C++. [caption width="800"] [/caption] When there is a need to return a 2D array from a function it is always hard to find
4 min read
Return by reference in C++ with Examples
Pointers and References in C++ held close relation with one another. The major difference is that the pointers can be operated on like adding values whereas references are just an alias for another variable. Functions in C++ can return a reference as it's returns a pointer.When function returns a reference it means it returns a implicit pointer. Re
3 min read
Java System.exit(0) vs C++ return 0
Java and C++ are languages with different applications and design goals. C++ is an extension of procedural programming language C and Java relies on a Java virtual machine to be secure and highly portable. This leads them to many differences. In this article, we will see the difference between C++ return 0 and Java System.exit(0). Before getting in
3 min read
Return type deduction in C++14 with Examples
In this article, we will discuss Return Type Deduction in C++14. Using an auto return type in C++14, the compiler will attempt to deduce the return type automatically. Program 1: C/C++ Code // C++14 program to illustrate the // return type deduction #include &lt;iostream&gt; using namespace std; // Function to multiply the two // numbers a and b au
3 min read
Practice Tags :