C Standard Library

C <errno.h> - EDOM constant



The C <errno.h> EDOM is macro constant which represents a domain error. The domain error occurs if an input argument is outside the domain of a mathematical function and sets errno to EDOM.

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.

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

In the <errno.h> header file, it is defined as follows:

#define EDOM /* implementation-defined */

Example 1:

The example below shows the usage of EDOM macro constant.

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <string.h>

int main(){
  double result = sqrt(-1.0);
  printf("%f\n", result);

  if (errno == EDOM) {
    printf("sqrt(-1.0) failed: %s\n", strerror(errno));
  }
}

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 constant is used to handle a domain error condition.

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <string.h>

int main(){
  double result = log(-1.0);
  printf("%f\n", result);
  
  if (errno == EDOM){
    fprintf(stderr, "Value of errno: %d\n", errno);
    perror("Error printed by perror");
    fprintf(stderr, "Error executing function: %s\n", strerror(errno));
  }
}

The output of the above code will be:

-nan

Value of errno: 33
Error printed by perror: Numerical argument out of domain
Error executing function: Numerical argument out of domain

❮ C <errno.h> Library