5

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!

John Topley
  • 113,588
  • 46
  • 195
  • 237
Julen
  • 1,574
  • 3
  • 22
  • 38

7 Answers7

6

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.
Philip Potter
  • 8,975
  • 2
  • 37
  • 47
  • It might be worth pointing out that on Linux if an application is terminated by a signal, its exit code will be the signal numerical value (e.g. 11 for SIGSEGV). But yeah, there's no single standard, apart from EXIT_SUCCESS and EXIT_FAILURE – sbk Mar 03 '10 at 14:06
2

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

Community
  • 1
  • 1
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
0

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.

Aurril
  • 2,469
  • 2
  • 24
  • 38
0

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.

Shrey
  • 679
  • 2
  • 6
  • 15
0
  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.
Kerido
  • 2,930
  • 2
  • 21
  • 34
0

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.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
0

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.

JohnMcG
  • 8,709
  • 6
  • 42
  • 49
  • This is very late. But would you say that the check condition in the script would be some kind of an if-else and hence the idea to return a 0 and break the if-else control? – viki.omega9 Jan 30 '13 at 05:40
  • 1
    Yeah, something like that. Or they can be strung together on a command line with && so a failure of the first command will short-circuit and prevent the second process from being called. – JohnMcG Feb 08 '13 at 18:18