Default value of errno variable

Joze picture Joze · Jan 9, 2014 · Viewed 8.4k times · Source

I want to use the errno library in order to define the return of a project (c language) functions. And I'm wondering about something...

I will do something like this :

#include <errno.h>

int myfunction (void)
{
  int res;

  /*Some actions....*/

  if(success)
    res = 0;
  else if (fail)
    res = -EIO;

  return res;
 }

I like to always initialize my local variable and I am wondering what should be the default value of a variable using the errno value (probably 0) ? But I don't really like to set by default : "SUCCESS" I prefer a "Failure" value. What is your point of view (or rule) about it ?

Thanks by advance.

Answer

Jens Gustedt picture Jens Gustedt · Jan 9, 2014

If your question is what the default, no error, value is for errno and all the functions that return error status in errno, that value is 0 as you expect.

But the error code is usually not returned as a negative value to the caller as you have it in your snippet.