EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Error Handling in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Error Handling in C

Error Handling in C

Introduction Error Handling in C

Error handling is the concept where it is conducted to respond to the occurrences of the error during computations of programs, applications, etc which also includes detection and process of how to resolve these errors. There are different ways of handling errors in different programming languages. In C programming language there is no support for error handling but instead, it provides methods and variables that are provided by the C standard library file called error.h which helps to find the errors and return to the function call. In general, the C language usually returns the function values in either -1 or NULL for error cases.

Working of Error Handling in C

As we know that error handling is not supported by the C programming language instead it has an error.h header file which provides few methods and variables that return values that are considered to detect the error occurrences while executing the programs. Therefore in any C program, it checks for the return value of the function to detect what kind of error has been occurred. It is usually better to follow the practice of assigning errno to 0 at the initializing a program which helps to indicate that there are no errors in the program.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Methods of Error Handling in C

In the C program, there are different functions or methods of error handling in C. They are specified as:

1. Global variable Errno

In C programming language, this is a variable where it is known as errno and is assigned a specific number or code that is used within the program to detect the type of error. Such type of error is declared in the header file known as error.h, so there are different error numbers for different types of errors and some of them are listed below:

Error number Error description
1 Operation not permitted
2 No such file or directory
3 No such process
4 Interrupted system calls
5 I/O error
6 No such device or address

Now let us see an example, to see what error value will be displayed if the file does not exists.

Below is the example are as follows:

Code:

#include <stdio.h>
#include <errno.h>
int main()
{
FILE * f;
f = fopen("article.txt", "r");
printf(" Value of error number as errno: %d\n ", errno);
return 0;
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

Error Handling in C Example 1

In the above program, we are trying to open a file that does not exist, and hence it will give an error that has been assigned a value and it is errno 2.

2. Perror() and strerror()

There are two different methods or functions in C, which are used to display the error message instead of just displaying the errno as we did it in the above program. They are as follows:

  • perror()

This function takes the message to be displayed which also displays the textual representation of errno.

Syntax:

void perror (const char *s)

s: – can be a string or message to be printed before the error message.

  • strerror()

This function points to the string or message or textual representation of the errno value and this function is defined in the header file string.h library.

Syntax:

char *strerror( int errornum)

errornum: this contains the error number i.e. errno.

The above two functions can be demonstrated by the below program. Below is the example are as follows:

Code:

#include <stdio.h>
#include <errno.h>
#include <string.h>
extern int errno ;
int main () {
FILE * f;
int errornum;
f = fopen ("article.txt", "rb");
if (f == NULL) {
errornum = errno;
fprintf(stderr, "The Value of errno: %d\n", errno);
perror("Error message that is printed by perror");
fprintf(stderr, "Error message for opening file that does not exist: %s\n", strerror( errornum ));
} else {
fclose (f);
}
return 0;
}

Output:

Error Handling in C Example 2

In the above program, we are trying to open a file that does not exist so to print the customized message for such error we using perror() and strerror() function which will print the error message along with errno with a customized error message.

3. Exit Status

In this, it provides an exit() function which takes two values for printing successful or unsuccessful termination by using EXIT_SUCCESS and EXIT_FAILURE. This exit() function is defined in the standard library stdlib.h header file. This also can be demonstrated by the below program.

Below is the example are as follows:

Code:

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
FILE * f;
f = fopen ("article.txt", "rb");
if (f == NULL)
{
printf("The Value of errno printed is : %d\n", errno);
printf("Error message printed while opening the file with errno: %s\n",
strerror(errno));
perror("Error message printed by perror");
exit(EXIT_FAILURE);
printf("The message will not be printed\n");
}
else
{
fclose (f);
exit(EXIT_SUCCESS);
printf("The message will be printed\n");
}
return 0;
}

Output:

Exit Status Example 3

4. Divide by Zero Error

The statement itself defines the error as this is displayed or occurred when the divisor is zero before a division command so this leads to dividing by zero error.

Example:

Code:

#include<stdio.h>
#include <stdlib.h>
void function(int);
int main()
{
int x = 0;
function(x);
return 0;
}
void function(int x)
{
float f;
if (x==0)
{
printf("Division by Zero is not allowed as it leads to the error");
fprintf(stderr, "Division by zero error\n");
exit(EXIT_FAILURE);
}
else
{
f = 10 / x;
printf("f(x) is: %.5f", f);
}
}

Output:

Zero Error Example 4

Conclusion

In this article, we conclude that the error handling in C programming language is not supported as to instead it provides few functions and error number values that are printed as error messages. In this article, we have seen the few error values list such that the number indicates a different type of error. In this article we have also seen few functions like perror(), strerror(), and exit() which prints the customized error message to the particular error type with errno.

Recommended Articles

This is a guide to Error Handling in C. Here we discuss the Introduction and Working of Error Handling in C along with Various Methods and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –

  1. Prime Numbers in C
  2. Reverse Number in C
  3. Reverse String in C
  4. Reverse String in PHP

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

2 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More