-1

I’m creating my own C library and I came across a dilemma.

What value should library function return?

Should I have my own errno variable, and then functions return 0 for success and -1 for failure and set errno to the specific error? (like most of libc functions do).

Or should I have an enum containing all the error codes and then every function returns the specific error number?

On the one hand I see the first method (errno) being used in lots of libraries, on the other hand I don’t see why a function returning int should waste its entire 4 bytes of return code for just 0 and -1 and use another global variable to specify the error? Why not return the error number directly?

I would like to get a better sense of why libraries use errno? I might be missing something here.

Thanks in advance :)

Tal Avraham
  • 300
  • 1
  • 6
  • "return code for just 0 and -1". Which functions are you referencing in particular? Many of the standard C functions return something else on success and only return -1 or some other error indicator on failure. In those cases it arguably makes sense to have a seperate `errno` so as to give the function a full range of return values for non-error reporting. – kaylum Dec 05 '19 at 09:50
  • 3
    It is the ultimate opinion based question. – n. m. could be an AI Dec 05 '19 at 09:56
  • Important thing is you should have a consistent convention in all your library functions, rest is up to you. – unlut Dec 05 '19 at 09:59
  • I don't think it's opinion based to say that global `errno` variable is horrible way to do error handling. – user694733 Dec 05 '19 at 10:04
  • 3
    @user694733 That is your opinion. – unlut Dec 05 '19 at 10:05
  • @user694733 `errno` nowadays usually isn't really a global, but a thread-local variable. – Ctx Dec 05 '19 at 10:16
  • 1
    @kaylum I think it’s strange that a function can either return a value, let’s say char*, or a failure code (NULL) and then set another errno variable to specify the error. Why not have the char* variable as an out parameter for the function, and return error code specifying the actual error from the function. – Tal Avraham Dec 05 '19 at 10:58

2 Answers2

1

From my experience of embedded software development (mostly bare-metal and low-level), errnois not used very often.

I've been usually working on libraries whose functions return 0 for success and a positive value for errors. The different error values may be defined as an enum, as macros or whatever.

The fact that 0 is the value for success make the error management convenient:

if ( function(...) )
{
    ... // handle the error
}
Guillaume Petitjean
  • 2,408
  • 1
  • 21
  • 47
0

The errno method has the advantage, that you only need one return value signaling failure (especially useful, when the range of the return value is largely used).

Furthermore, it allows to have functions like perror(), that use errno implicitly.

It is not advisable to mix both variants (-1/errno vs. directly returning error codes), because the error handling is very individual for each function then.

Ctx
  • 18,090
  • 24
  • 36
  • 51