errors/error-1.c - Et eksempel på brug af errno fra errno.h. | Lektion 7 - slide 10 : 25 Program 1 |
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <errno.h> int main(void) { double input = -1.0, x = 0.0; /* Take square root of a negative number, prepare for error reporting */ errno = 0; /* Reset errno */ x = sqrt(input); /* sqrt assigns errno in case of error */ if (errno){ /* errno is positive in case of an error */ printf("sqrt failed, code: %d, message: '%s'\n", errno, strerror(errno)); x = 0.0; } printf("Result is %f\n", x); return 0; }