Julen
Julen

Reputation: 1604

What does returning zero by convention mean?

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?

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!

Upvotes: 5

Views: 6322

Answers (7)

JohnMcG
JohnMcG

Reputation: 8815

This is a remnant of the C/Unix legacy.

If your program is being called within a script, is is useful for the script to know whether your program was successful. Thus, was born the convention of returning 0 for success from executable programs.

C did not have native boolean types, so this was an integer. And this allowed us to return different numbers for different kinds of errors.

This convention then spread to regular functions, since, as others have posted, C/C++ did not originally have exceptions, and even once we had exceptions, it took discipline to use them correctly, and there was a lot of legacy code (and legacy programmers) using the return value convention.

If all you're interested in is success/failure, then 0 for failure and 1 for success will do. For internal return values, even if there are currently only two values, I would advise using an enum type instead since bi-states have a way of evolving into tri-states, and it will be easier to follow what you're doing.

Upvotes: 0

Dan Tao
Dan Tao

Reputation: 128357

Originally the return value would have corresponded to an error code. Imagine there's a big table of all the possible erroneous outcomes a program may have. The program terminates; you want to know what, if anything, went wrong. If a non-zero integer were returned, you'd look it up in your table of error codes. If 0 is returned, it's equivalent to "nothing went wrong."

I only put it that way because sometimes people find it unintuitive that 0 means something good. It's kind of like the glass is totally empty... of poison.

Upvotes: 0

Philip Potter
Philip Potter

Reputation: 9135

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.

Upvotes: 6

Kerido
Kerido

Reputation: 2940

  1. The meaning of zero. It really depends on the class/function design. It should be part of the documentation how clients must treat the return value. For example, COM methods return zero to indicate successful operation (there is the S_OK constant equal to zero). Windows file operations (CreateFile, etc.) return non-zero in case of a successful operation. You see, it depends. The only convention that might be about it is: the code must be documented!!!
  2. What's better to return. I think, int may be better for more complicated functions (where you might need to analyze a potential error). Since int and bool are typically returned in the EAX register (assuming you are on the x86 architecture), no extra memory is required to return an int instead of a bool.

Upvotes: 0

Shrey
Shrey

Reputation: 689

This is not a rule, but more of a design decision (though, if I recall correctly, some standards like POSIX have their definitions)

0 as a return value is considered as 'Normal' or 'OK. Where as, when you return something other than 0, it can be OK or Error. For e.g., if you issue a read() call, it may return the number of bytes read as a positive value - which is certainly not an error. But, if it returns 0 it means file end. Any negative value from it would mean some error in reading.

Upvotes: 0

Aurril
Aurril

Reputation: 2469

In early C there was no exception concept. So the programmers helped themselves by using error codes and defined 0 as no error. All other error codes have their own meaning, defined by the programmer.

This scheme was taken over to C++. But nowadays you also have the concept of throwing exceptions on errors (which is a better OOP Style). That main still uses int as return value has historical reasons.

Upvotes: 0

Anders Abel
Anders Abel

Reputation: 69270

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++?

Upvotes: 2

Related Questions