5

Most of the places I have seen the return code values are done like this,

for success status return , #define SUCCESS 0 and other no zero numbers for all other error cases.

My question is , why we selecetd zero for SUCCESS case? Is there any specific programming best practice concerns for that?

/R

Renjith G
  • 4,718
  • 14
  • 42
  • 56

4 Answers4

4

It's an old C habit to return 0 for success and some other code for errors, so you can say:

int error = do_stuff();
if (error) {
    handle_error(error);
}

However, predicates usually work the other way around, returning 1 for "success" (or true from <stdbool.h>).

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • This is an OS convention. It has nothing to do with C. – Šimon Tóth Sep 20 '11 at 10:22
  • 1
    @Let_Me_Be: that depends on whether the OP refers to program or function return codes. If this were an OS issue, then instead of defining `SUCCESS`, the programmer should have used `EXIT_SUCCESS` from the stdlib. – Fred Foo Sep 20 '11 at 10:23
  • 1
    And this way you can distinguish errors as well. `ret = func();` if(!ret) { success();} else if(ret == AUTH_ERROR) { .. handle auth error } else if (ret == COMM_FAILURE} { retry_connection(); } etc..` – nos Sep 20 '11 at 11:07
  • Yes, that's what the `handle_error(error)` is supposed to do in the example :) – Fred Foo Sep 20 '11 at 11:22
3

Zero often means success because zero is the only integer value that evaluates to false. All other integer values evaluate to true and they are meant for various error codes.

It might seem a bit weird and logically inverted, but because success is just success and errors can be different, the above convention is chosen often.

In fact, this is the most rational convention if the return value of a function is used not only as a success/failure indicator, but as an error code too. If the error code is stored, say in an additional output parameter, then returning 1 for success and 0 for failure makes more sense.

Imagine what the condition would be in the case of 0 being success:

if (errcode = func()) {
  /* error handling */
}

versus the more cumbersome:

if ((errcode = func()) != 1) {
  /* error handling */
}
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
3

You have to differenciate between different errors, there can be MaxInt - 1 different ones.

If 0 was an error and 1 was success, how could you tell the difference between all errors?

wormsparty
  • 2,481
  • 19
  • 31
  • if Zero is the error case then I will use an if to check the error case – Renjith G Sep 20 '11 at 10:38
  • But how can you distinguish what kind of error occured, if 0 is the only thing that indicates an error ? Often you want to know more then "an error occured" You want to know specifically what failed. You'd either have to set a global variable to the last failure code of a function, or pass the error out to the caller through pointer in the function argument list. Both of which are cumbersome compared to just returning the error code – nos Sep 20 '11 at 11:12
2

This is an operating system convention.

http://en.wikipedia.org/wiki/Exit_status

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • 1
    That describes the exit code of a program though, not why it's common (and sensible) to have functions return 0 to indicate success. – nos Sep 20 '11 at 11:05
  • 1
    It's not only used in the exit status of programs. Generally `0` means success when you want to use the return code not only as a success/failure indicator, but as an error code as well. Then you can easily distinguish the two cases of having or not having an error. – Blagovest Buyukliev Sep 20 '11 at 11:10