10

I have my own return type and functions defined like this:

typedef enum xx_return_t {
   success = 0,
   general_error = -1,
   specific_error = -2,
   [...]
} xx_return_type;

xx_return_type generalFunction(void) {
   if(there_was_an_error)
      return general_error;
}

However I'm a bit uncertain on the error type values here; what is standard/best practice for values of error returns in C/C++ - negative or positive?

Update: Thank you for your answers! I was looking for info on both C and C++, but I also realize this raises good questions on general structure and methods specific to each language (exceptions, error codes, object returns, etc).

1
  • 1
    Are you asking about C or C++?
    – default
    Oct 4, 2012 at 13:21

5 Answers 5

19

This is really two totally different questions (C and C++ versions) disguised as one.

In C++ the answer is simple: Use return values for...returned data values and use exceptions for error/exception cases. Don't use return values for error checking in pure C++ (a C library API to C++ is a different story).

In C you don't have that option, so I would suggest using 0 for success and negative numbers for error codes. This leaves the flexibility, if desired, to use positive numbers for additional success information (for example read calls)

2
  • 2
    Regarding exceptions in C++, I would note that it does not mean that suddenly all methods should throw. For example: T const* MyMap::find(std::string const& name); is a method that need not throw when the name was not found; it can just return a null pointer. Oct 4, 2012 at 14:48
  • I agree with Matthieu. Exceptions should be thrown only in case of exceptional events, not ordinary negative results of a search, etc. Throwing exception is much slower that returning value from function and even if it is not thrown the try{}catch() block itself and inclusion of all exception handling machinery makes the program slower.
    – 4pie0
    Mar 25, 2015 at 11:45
4

C++:

  1. It's a matter of choice, I've seen both.
  2. Use exceptions!
8
  • 4
    "2." works especially well for this c tag ;-) Oct 4, 2012 at 13:18
  • 4
    (Wish there was a +0.5 option ;-)) Oct 4, 2012 at 13:18
  • 3
    @MichaelKrelin-hacker: It's always a problem with the C/C++ questions. Either it's C or it's C++... Oct 4, 2012 at 13:21
  • 1
    @MatthieuM., I'd say often, not always. The question here is not whether it's c or c++, but rather whether it's "c and c++" or "c or c++" ;-) Oct 4, 2012 at 13:23
  • 4
    @MatthieuM. Just like Stroustrup said: "I dont know of any language called C/C++" :)
    – Brady
    Oct 4, 2012 at 13:23
3

There are several conventions to choose from:

  • microsoft uses 0=FALSE=error for most of its high level api's. and let the user lookup the specific error with a global function GetLastError()
  • microsoft uses 0=ok for lower level apis, like registry, audio, or telephony. these all return a type similar to HRESULT, containing the specific error code.
  • posix functions returning a status usually return -1 for error, and let the user lookup the error via a global variable errno
  • functions returning a pointer usually return NULL for error
  • c++ STL functions return 'end' for status like 'not found'. errors like 'out-of-memory' are signalled using an exception.

In general, it is more important that you use your convention consistently, than which convention you use.

Btw, some examples of how not to do this can be found in microsoft's header files:

#define S_OK       ((HRESULT)0x00000000L)
#define S_FALSE    ((HRESULT)0x00000001L)

Also beware of two different error values for HANDLE's in windows: either 0 or INVALID_HANDLE_VALUE

2

I don't know about C...

... but in C++ the idea is not to use error codes, in general.

I do not mean that you should use exceptions, but an error code is not really too informative (lacks context, what is the worth of FILE_NOT_FOUND when the file name is unknown ?).

In the instances that I did not use exceptions, I tended to prefer full blown error objects. An example could be:

boost::variant<File, Error> open(std::string const& filename, FileMode mode);

where you will get either a file or an error, depending on what's happening.

1

Better to use 0 (zero) and non-zero so they can also be considered boolean. Typically false is 0, and true is anything else.

10
  • That's what he's doing & non-zero can be negative and positive... Oct 4, 2012 at 13:17
  • 1
    @LuchianGrigore Actually, he's doing the opposite. As you mentioned, both are common, but its nice to be able to also use true/false.
    – Brady
    Oct 4, 2012 at 13:21
  • 3
    @Brady, false is not opposite of success, false is the opposite of true. But now I see what you mean. Did you know that 99% of standard c-library calls and syscalls return 0 for success and error code otherwise and are very convenient as booleans? Oct 4, 2012 at 13:27
  • 1
    I disagree. Normally, there's one way to succeed and many reasons for failure. You're still entitled for your opinion, of course, but I'm still thinking for which answer to vote and it's not yours (sorry;)). Oct 4, 2012 at 13:33
  • 1
    Yup. You suggest the world full of success and but one way to fail :) (as for treating it as a boolean, it would be a matter of ! if in fact we weren't mostly interested in failure — as in if(dostuff()) return non_zero_oh_no_while_doing_stuff_code;). Oct 4, 2012 at 14:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.