Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4 — Error Handling in C

Lesson 31 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Error Handling in C

C does not have built-in exception handling like C++ or Java. Instead, error handling is done through return values, errno, and error-reporting functions.

---

Return Value Conventions

Functions signal errors by returning special values:

FILE *fp = fopen("file.txt", "r");
if (fp == NULL) {
    /* error: file could not be opened */
}

int *arr = (int *)malloc(100 * sizeof(int));
if (arr == NULL) {
    /* error: out of memory */
}

Common conventions:

  • Return NULL pointer on failure (pointer-returning functions)
  • Return -1 or negative value on failure (int-returning functions)
  • Return 0 for success (e.g., main)

---

errno — Error Number

The global variable errno (from <errno.h>) is set by system calls and library functions when an error occurs.

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

FILE *fp = fopen("missing.txt", "r");
if (fp == NULL) {
    printf("Error code: %d\n", errno);
    printf("Error: %s\n", strerror(errno));
}

---

perror() — Print Error Message

FILE *fp = fopen("missing.txt", "r");
if (fp == NULL) {
    perror("fopen");
    /* Output: fopen: No such file or directory */
}

---

strerror() — Get Error String

#include <string.h>
printf("%s\n", strerror(errno));

---

Common errno Values

ValueMacroMeaning
1EPERMOperation not permitted
2ENOENTNo such file or directory
12ENOMEMOut of memory
13EACCESPermission denied
22EINVALInvalid argument

---

assert() — Debug Assertions

#include <assert.h>

int divide(int a, int b) {
    assert(b != 0);   /* aborts program if b == 0 in debug mode */
    return a / b;
}
  • Disabled in release builds by defining NDEBUG
  • assert(expr) — if expr is false, prints message and calls abort()

---

Error Handling Pattern

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

int processFile(const char *filename) {
    FILE *fp = fopen(filename, "r");
    if (!fp) {
        perror("fopen");
        return -1;   /* signal error */
    }
    
    char line[256];
    while (fgets(line, sizeof(line), fp)) {
        printf("%s", line);
    }
    
    if (ferror(fp)) {
        perror("fgets");
        fclose(fp);
        return -1;
    }
    
    fclose(fp);
    return 0;   /* success */
}

int main() {
    if (processFile("data.txt") != 0) {
        fprintf(stderr, "Failed to process file\n");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}