1. <errno.h> Error Codes Reported by Library Functions
The header <errno.h> defines macros to report error conditions through error codes.
The errno macro expands to an external variable of type int, containing the last error code generated in any function using the errno facility.
The following macros expand to integer constants which represent the error codes:
domain errors | e.g.) sqrt(-1.0) (in <math.h>) | |
ERANGE | range errors | e.g.) atoi("12345678901") (in <stdlib.h>) |
If you intend to test the value of errno after library function calls, first set it to 0, because the library functions do not reset the value to 0.
perror or strerror functions can be used to print the description of the message associated with a particular errno.
void perror(const char *s) (in <stdio.h>) perror(s) prints s and an implementation-defined error message corresponding to errno, as if by fprintf(stderr, "%s: %s\n", s, "error message") |
char *strerror(int e) (in <string.h>) strerror(e) returns a pointer to an implementation-defined error message corresponding to e. |
EXAPMLE CODES
1. error.h
l err.c
#define _CRT_SECURE_NO_DEPRECATE
// to disable deprecation for standard C functions in VC
// must be defined before headers are included
#include <errno.h>
#include <stdio.h> // perror
#include <string.h> // strerror
#include <stdlib.h> // atoi
#include <math.h> // sqrtvoid main() {
printf("EDOM = %d, ERANGE = %d\n\n", EDOM, ERANGE);
printf("[At Startup]\n");
printf("errno = %d\n", errno);
perror("ERROR");
fprintf(stderr, "%s: %s\n\n", "ERROR", strerror(errno));
printf("errno = %d\n", errno = ERANGE);
perror(NULL);
perror("ERROR");
fprintf(stderr, "%s: %s\n\n", "ERROR", strerror(errno));
printf("sqrt(-1.0) = %g\n", sqrt(-1.0));
fprintf(stderr, "ERROR: %d -- %s\n\n", errno, strerror(errno));
printf("sqrt(4.0) = %g\n", sqrt(4.0));
fprintf(stderr, "ERROR: %d -- %s\n\n", errno, strerror(errno));
errno = 0; // clear error
printf("sqrt(4.0) = %g\n", sqrt(4.0));
fprintf(stderr, "ERROR: %d -- %s\n\n", errno, strerror(errno));
errno = 0;
printf("atoi(\"12345678901\") = %d\n", atoi("12345678901"));
fprintf(stderr, "ERROR: %d -- %s\n\n", errno, strerror(errno));
}
// to disable deprecation for standard C functions in VC
// must be defined before headers are included
#include <errno.h>
#include <stdio.h> // perror
#include <string.h> // strerror
#include <stdlib.h> // atoi
#include <math.h> // sqrtvoid main() {
printf("EDOM = %d, ERANGE = %d\n\n", EDOM, ERANGE);
printf("[At Startup]\n");
printf("errno = %d\n", errno);
perror("ERROR");
fprintf(stderr, "%s: %s\n\n", "ERROR", strerror(errno));
printf("errno = %d\n", errno = ERANGE);
perror(NULL);
perror("ERROR");
fprintf(stderr, "%s: %s\n\n", "ERROR", strerror(errno));
printf("sqrt(-1.0) = %g\n", sqrt(-1.0));
fprintf(stderr, "ERROR: %d -- %s\n\n", errno, strerror(errno));
printf("sqrt(4.0) = %g\n", sqrt(4.0));
fprintf(stderr, "ERROR: %d -- %s\n\n", errno, strerror(errno));
errno = 0; // clear error
printf("sqrt(4.0) = %g\n", sqrt(4.0));
fprintf(stderr, "ERROR: %d -- %s\n\n", errno, strerror(errno));
errno = 0;
printf("atoi(\"12345678901\") = %d\n", atoi("12345678901"));
fprintf(stderr, "ERROR: %d -- %s\n\n", errno, strerror(errno));
}
> err
[At Startup]
errno = 0
ERROR: No error
ERROR: No error
errno = 34
Result too large
ERROR: Result too large
ERROR: Result too large
sqrt(-1.0) = -1.#IND
ERROR: 33 -- Domain error
sqrt(4.0) = 2
ERROR: 33 -- Domain error
sqrt(4.0) = 2
ERROR: 0 -- No error
atoi("12345678901") = 2147483647
ERROR: 34 -- Result too large
n Output of infinite and indeterminate values
s -1.#IND : indeterminate value
s ±1.#INF: infinity – e.g.) double x = 0.0; printf("%g", 1.0 / x);
No comments:
Post a Comment