• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 6 pages
Other

Error Handling in C: Techniques for Managing Errors with Examples

Document preview thumbnail
Preview 2 out of 6 pages

This document explains how to handle errors in C programming using techniques like errno, perror(), and exit(). Learn how to manage runtime errors, file errors, and system errors with practical examples. Ideal for second-year Computer Science students, this guide will help you improve the reliability of your programs.

Content preview

Error Handling in C
Error handling in C is an essential concept to ensure robust and reliable programs.
Unlike some modern programming languages, C does not provide built-in support
for error handling, such as exceptions. Instead, error handling in C is implemented
using traditional mechanisms like return values, error codes, and global variables.

1. Using Return Values for Error Handling
Functions in C often return a specific value to indicate success or failure. This
value can be checked to handle errors appropriately.

Example: Checking Return Values

#include <stdio.h>

int divide(int a, int b, int* result) {
if (b == 0) {
return -1; // Return an error code for division by zero
}
*result = a / b;
return 0; // Success
}

int main() {
int a = 10, b = 0, result;

if (divide(a, b, &result) != 0) {
printf("Error: Division by zero\n");
} else {
printf("Result: %d\n", result);
}

return 0;
}

, In this example, the divide function returns -1 to indicate an error (division by
zero), and the caller checks the return value.

2. Global Variable errno
The <errno.h> header file provides a global variable errno that stores error codes
set by certain library functions. These codes indicate the type of error
encountered.

Common Error Codes

 EIO: Input/output error
 ENOMEM: Out of memory
 EINVAL: Invalid argument
 EBADF: Bad file descriptor

Example: Using errno

#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
FILE* file = fopen("nonexistent.txt", "r");
if (file == NULL) {
printf("Error: %s\n", strerror(errno)); // strerror() provides a readable error
message
}

return 0;
}

In this example, attempting to open a nonexistent file sets errno. The strerror()
function converts the error code to a human-readable message.



3. Custom Error Codes

Document information

Uploaded on
January 21, 2025
Number of pages
6
Written in
2024/2025
Type
Other
Person
Unknown
$6.39

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions