1
int main()
{
  /*

   Code Here

  */

  return 0;
}

If apply return -1 instead of 0 what will happen ? I use -1 also with return but my program also run successfully. Than what is the need of return statement with number.

  • 2
    This value is returned back to the operating system as a process exit code. Depending on your platform there are different ways to analyze it. – SergeyA Oct 15 '18 at 18:54
  • 1
    Btw, why would this question be downvoted? I see nothing inherently wrong with it, it is healthy wondering. – SergeyA Oct 15 '18 at 18:55
  • 1
    ***If apply return -1 instead of 0 what will happen ?*** the calling program (program that started your program) can get this value and see that your program failed. What it chooses to do with that code is up to the calling program. If you execute your program from a shell (or command prompt on windows), usually the shell will allow you to get the exit code into a variable. – drescherjm Oct 15 '18 at 18:55
  • Return 0 usually stands for a successful execution of your code. It is not required in C++. The compiler automatically adds it. -1 would mean there was something wrong with the execution of your code. – hshantanu Oct 15 '18 at 18:55
  • @SergeyA I can't be sure, but I suspect people downvoted because they suspected the question was a duplicate (as indeed it is). – Steve Summit Oct 15 '18 at 18:57
  • note that c and c++ are two different languages. Having different rules for `return` from `main` is not an exception – 463035818_is_not_an_ai Oct 15 '18 at 18:57
  • The return of of function is generally handled by the system, It can express success or failure or something else. let's say another routine is interested in the return value of your program... – Raindrop7 Oct 15 '18 at 18:59
  • @SteveSummit by just being a duplicate the question is not necessarily low quality. Searching on Stack Overflow is not for the faint of heart, so it is understandable when people can't quite figure it out. – SergeyA Oct 15 '18 at 19:03
  • @SergeyA But you didn't ask "Is there a *good* reason this question was downvoted?" (In other words: it may be a poor reason, but my suspicion stands.) – Steve Summit Oct 15 '18 at 19:11
  • @SteveSummit fair enough. – SergeyA Oct 15 '18 at 19:14

1 Answers1

0

Conventionally, the return value is used to determine whether a program has succeeded or failed. You can call your program from another program and record the status. The operating system records the return value of a program and infers 0 as success and non-zero as failure.

An example: if you have scheduled your program in Task Scheduler. It will interpret 0 as success and any other value as failure and display the status column of your task accordingly.

VHS
  • 9,534
  • 3
  • 19
  • 43
  • The operating system applies whatever rules the operating system applies. The C and C++ standards do **not** assume that a return code of 0 means success and a non-0 code means failure. There are three valid return values in C and C++. `return EXIT_SUCCESS` means that the program succeeded. `return EXIT_FAILURE` means that the program failed. And as a special case, `return 0` means that the program succeeded. But `return 0` does not necessarily give a return code of 0 to the OS. It gives whatever the OS treats as success, which in some cases is all 1s, in others (the ones most of us use), 0. – Pete Becker Oct 16 '18 at 13:36
  • @PeteBecker, that's interesting to know. Thank you so much for additional information. – VHS Oct 17 '18 at 14:05