0% found this document useful (0 votes)
63 views

C Programming Language

In C programming, there is no direct support for error handling so programmers must detect and handle errors. When a function fails, its return value indicates success or failure and errors should be handled or logged. Common Linux errors provide error numbers but not descriptions, so it is important to understand what each error number means. The C external variable errno and functions like perror() and strerror() can be used to get error numbers and their descriptions to properly handle errors. The code example demonstrates using these functions to open a file and check for errors.

Uploaded by

Be Be1111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

C Programming Language

In C programming, there is no direct support for error handling so programmers must detect and handle errors. When a function fails, its return value indicates success or failure and errors should be handled or logged. Common Linux errors provide error numbers but not descriptions, so it is important to understand what each error number means. The C external variable errno and functions like perror() and strerror() can be used to get error numbers and their descriptions to properly handle errors. The code example demonstrates using these functions to open a file and check for errors.

Uploaded by

Be Be1111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Programmers should handle all kinds of errors to protect the program from failure.

In C programming language, there is no direct support for error handling. You have to
detect the failure and handle the error. In C programming language, return values
represents success or failure. Inside a C program, when a function fails, you should
handle the errors accordingly, or at least record the errors in a log file.
When you are running some program on Linux environment, you might notice that it
gives some error number. For example, “Error no is : 17”, which doesn’t really say much.
You really need to know what error number 17 means.

This article shows all available error numbers along with it descriptions. This article
might be a handy reference for you, when you encounter an error number and you
would like to know what it means.

 In C programming language, there is an external variable called “errno”.


 From this errno variable you can use some error handling functions to find
out the error description and handle it appropriately.
 You have to include errno.h header file to use external variable errno.
 perror function prints error description in standard error.
 The strerror function returns a string describing the error code passed in the
argument errnum.
The following C code snippet tries to open a file through open system call. There are two
flags in the open call. O_CREAT flag is to create a file, if the file does not exist. O_EXCL
flag is used with O_CREAT, if the file is already exist open call will fail with the proper
error number.

$ cat fileopen.c

#include <stdio.h>

#include <fcntl.h>

#include <errno.h>

#include <string.h>

main()

// Declaration of a file descriptor


int fd;

// Opening a file

fd = open("/root/sasikala/testing",O_CREAT|O_EXCL);

// If Open is failed

if ( fd < 0 ) {

printf("Opening file : Failed\n");

printf ("Error no is : %d\n", errno);

printf("Error description is : %s\n",strerror(errno));

// If Open is success

else

printf("Opening file : Success\n");

$ cc -o fileopen fileopen.c

$ ./fileopen

Opening file : Success


$ ./fileopen

Opening file : Failed

Error no is : 17

Error description is : File exists

At first execution, open got executed successfully, and it created the file since the file
was not available. In next execution, it throws an error number 17, which is “File already
exist”.

You might also like