Answer: In fact, we write C programs and request, for example, open a file for writing into it, to execute a some command etc., with Operating System and O.S., then, attempts to perform the requested work for us. What happens if O.S. fails to perform? Some error occurs. How’s this error reported to the user? Error, if any, occurs is reported to user by setting an external integer variable called ‘errno’ to an error code. The stderr() takes one of these error codes as an argument and returns a pointer to a massage describing the error. The function is prototyped below:
char *strerr(int errno);
Let’s now consider a simple program,
/* strerr.c -- program shows a pretty easy implementation of strerr() */ #include <stdio.h> #include <string.h> int main(void) { int errno = 0; /* errno set to 0 */ /* series of 'puts' calls */ puts("Let's"); puts("check"); puts("the"); puts("behaviour"); puts("of"); puts("strerr()"); puts("function."); fprintf(stderr, "\nputs STATUS: %s\n", strerror(errno)); return 0; }
Output is as follows:
Lets check the behaviour of strerr() function. puts STATUS: Success
Notice that above program succeeded! However, there are chances that program fails for some errors. In general, the best way to detect the errors is to test the return values of functions.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for Computer Science Internship
- Check C Books
- Practice Computer Science MCQs
- Practice BCA MCQs
- Watch Advanced C Programming Videos