Names specified here
Name Description Notes Source Availability
errno_t Error-number type ? T Headers C11
perror() Report last error (·) <stdio.h> C89 C90 C95 C99 C11
strerror() Interpret error number (·) <string.h> C89 C90 C95 C99 C11
strerror_s() Interpret error number ? (·) <string.h> C11
strerrorlen_s() Get length of message interpreting error number ? (·) <string.h> C11
Names specified for <errno.h>, “Error status”
Name Description Notes Source Availability
EDOM Domain-error indicator M <errno.h> C89 C90 C95 C99 C11
EILSEQ Indicator of illegal multibyte sequence M <errno.h> C95 C99 C11
ERANGE Range-error indicator M <errno.h> C89 C90 C95 C99 C11
errno Holder of last error number <errno.h> C89 C90 C95 C99 C11

This header is available in C89, C90, C95, C99 and C11.

Many standard library functions, when they report an error, also set what appears to be (and may actually be) a global variable called errno, which has type int, and holds an error code. A correct declaration for errno is available in <errno.h>. To use it properly, you need to adopt the following idiom:

errno = 0;
double result = atan2(y, x);
if (errno) {
  /* Handle error. */
}
[ Work in progress : Rubbish! Fix this! Actually, it might be appropriate for atan2 and other functions in <math.h>, but there are plenty of cases where a difinitive return code tells you there's an error.]

errno is not set if there is no error, and might not be set even if there is! (math_errhandling might be able to tell you more.) A number of error codes are defined, but zero is never one of them, so this is a safe default. The standard defines a small number of codes, like EDOM and ERANGE, but implementations may define many more, all with names beginning with E. The standard error codes are:

Macros with names matching ^E[0-9A-Z] might be added to <errno.h>.

errno_t is an optional type alias for int, and is used in the return types of some functions, to document that error codes are returned, rather than setting the global errno. __STDC_LIB_EXT1__ can be used to determine the existence of errno_t.

[ Work in progress : Actually, many of the functions that return errno_t are specified as simply returning non-zero on error, not requiring it to be one of the E-prefixed codes. So the analogy with errno might not be generally correct.]

Several functions are available to convert an error code into a locale-specific string:

#include <string.h>
char *strerror(int errnum);
#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>
errno_t strerror_s(char *s, rsize_t maxsize, errno_t errnum);
size_t strerrorlen_s(errno_t errnum);

strerror takes an error code, and returns a pointer to a message describing the error, according to the LC_MESSAGES locale category. strerror_s generates the same message, but writes it into the array of at least maxsize bytes at s, returning zero if the array was big enough, or non-zero if not. Provided maxsize is not zero, a null-terminated string will always be written into the array, possibly truncated. strerrorlen_s can be used to find out how big the array needs to be for a specific error code, not including the terminating null character.

#include <stdlib.h>
void perror(const char *s);

perror reads the current value of errno, and writes to stderr the message that strerror would provide. The null-terminated string s also forms part of the message (e.g., perror("Computing angle") could write Computing angle: Domain error), if not null or zero-length.


CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2023-10-04T20:24:03.201+0000