How are Errors Reported in a C Program When Operating System Fails to Perform some Requested Job

Question: How are Errors Reported When Operating System Fails to Perform some Requested Job in a C Program?

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:

advertisement
advertisement
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.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.