C++ Standard Library C++ STL Library

C++ <cerrno> - errno



The C++ <cerrno> errno is a preprocessor macro used for error indication. It expands to a static (until C++11) thread-local (since C++11) modifiable lvalue of type int.

errno is set to zero at program startup, and any function of the standard C library can modify its value to some value different from zero, generally to signal specific categories of error (no library function sets its value back to zero once changed).

A program can also modify its value. Hence, a user can modify its value or reset to zero at convenience before the call (since any previous call to a library function may have altered its value).

The <cerrno> header file also defines at least the following macro constants with values different from zero:

MacrosDescription
EDOMDomain error: The domain of a mathematical function is the real value for which the function is defined. For example, the square root function is defined only for non-negative values, therefore when the sqrt() function is called with a negative argument it sets errno to EDOM.
ERANGERange error: The range of values that can be represented with a variable is limited. For example, mathematical functions such as pow() can easily outbound the range representable by a floating point variable, or functions such as strtod() can encounter sequences of digits longer than the range representable values. In these cases, errno is set to ERANGE.
EILSEQIllegal sequence: Multibyte character sequence may have a restricted set of valid sequences. When a set of multibyte characters is translated by functions such as mbrtowc(), errno is set to EILSEQ when an invalid sequence is encountered.

Along with this, the majority of the macros defined by this header file were adopted by C++11 available in POSIX environments. For a full list, see list of macros in <cerror>.

The particular error messages associated with values of errno can be obtained using strerror() or directly printed using function perror().

In C++, errno is always declared as a macro, but in C it is unspecified whether errno is a macro or an identifier declared with external linkage. C11 fixes this, requiring that it be defined as a macro.

In the <cerrno> header file, it is defined as follows:

#define errno /* implementation-defined */

Example 1:

The example below shows the usage of errno macro.

#include <iostream>
#include <cmath>
#include <cerrno>
#include <cstring>

using namespace std;

int main(){
  double result = sqrt(-1.0);
  cout<<result<<"\n";

  if (errno == EDOM) {
    cout<<"sqrt(-1.0) failed: "<<strerror(errno)<<"\n";
  }
}

The output of the above code will be:

-nan
sqrt(-1.0) failed: Numerical argument out of domain

Example 2:

Consider one more example where this macro function is used to handle an error condition when tried to open a file which does not exist.

#include <cstdio>
#include <cmath>
#include <cerrno>
#include <cstring>

int main(){
  FILE * pFile;
  pFile = fopen ("wrongfile.txt", "r");
  
  if (pFile == NULL){
    fprintf(stderr, "Value of errno: %d\n", errno);
    perror("Error printed by perror");
    fprintf(stderr, "Error opening file: %s\n", strerror(errno));
  } else {
    fclose (pFile);
  }
}

The output of the above code will be:

Value of errno: 2
Error printed by perror: No such file or directory
Error opening file: No such file or directory

❮ C++ <cerrno> Library