Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does returning zero by convention mean?

Tags:

c++

This is likely a stupid question but I always find myself wondering which is the standard.

In most (not to say all) C++ first examples you may see the main function returning 0 value. This means the operation went ok or not?

  • 0 --> OK
  • 1 --> No OK.
  • Other --> ?

Which is the standard way of doing it?

By the way, is it better to return an integer or a boolean in this case?

Thank you guys!

like image 349
Julen Avatar asked Mar 03 '10 12:03

Julen


People also ask

What is the purpose of the return 0?

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.

Do you always need to return 0 in C?

The main function is generally supposed to return a value and after it returns something it finishes execution. The return 0 means success and returning a non-zero number means failure. Thus we "return 0" at the end of main function. But you can run the main function without the return 0.It works the same .

What happens if you dont return 0?

Short Answer: Nothing. Better Answer: return 0 it's used in main like a signal for know the exit of program was a success when return 0 executes. Best Answer: Still nothing because compilers already "put" return 0 in the the end of your code if you not explicit.

What is the difference between return 0 and return 1?

in main function return 0 or exit(0) are same but if you write exit(0) in different function then you program will exit from that position. returning different values like return 1 or return -1 means that program is returning error .


2 Answers

0 or EXIT_SUCCESS means success. EXIT_FAILURE means failure. Any other value is implementation defined, and is not guaranteed to be supported. In particular, std::exit(1) or return 1; are not actually guaranteed to indicate failure, although on most common systems they will.

EXIT_SUCCESS and EXIT_FAILURE are defined in <cstdlib>.

Edit: I suppose it might be useful to give a system-specific example:

The GNU make utility returns an exit status to the operating system:

  • 0: The exit status is zero if make is successful.
  • 2: The exit status is two if make encounters any errors. It will print messages describing the particular errors.
  • 1: The exit status is one if you use the `-q' flag and make determines that some target is not already up to date.

The ability to set multiple different values of failure means you can specify exactly how your program failed. There are two caveats, however:

  • There is no convention for failure status codes, afaik
  • This will work on "normal" OSes (windows, os x, unix), but it's not guaranteed by the C++ standard; so it might not work if you tried porting to VMS or some embedded system.
like image 141
Philip Potter Avatar answered Sep 29 '22 15:09

Philip Potter


0 is ok, other values are an error code. The main function should return an int and nothing else according to the standard. The question has been discussed before at What should main() return in C and C++?

like image 36
Anders Abel Avatar answered Sep 29 '22 15:09

Anders Abel