return 0 vs return 1 in C++ - GeeksforGeeks (2024)

Table of Contents
C++ C++ Please Login to comment...

Read

Improve

Improve

Improve

Like Article

Like

Save Article

Save

Report issue

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-casereturn 0return 1
In the main functionreturn 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 functionreturn 0 means that the user-defined function is returning false.return 1 means that the user-defined function is returning true.

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated : 20 Jun, 2022

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

return 0 vs return 1 in C++ - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 6221

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.