C Error Handling

Advanced
6 min read
Updated: Nov 2026
1 example

What Is Error Handling?

C has no exceptions. Functions report failure through return values (NULL, -1, an error enum), the global errno, or output parameters. Callers must check them. A common pattern is a single cleanup path with goto so every resource is released on every error.

Remember
enum { OK = 0, ERR_DIV0 = -1 };
int divide(int a, int b, int *out) {
    if (b == 0) return ERR_DIV0;
    *out = a / b;
    return OK;
}

Cheat Sheet

Keep these facts handy while you work with Error Handling in C.

TopicDetail
Return codesZero for success, negative for error
errnoSet by many library functions
CleanupOne exit label with goto
Exit statusreturn from main or exit(code)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: errno and perror().

Example 1 — Return an error code instead of crashing

The result goes through a pointer; the return value signals success.

C
#include <stdio.h>

enum { OK = 0, ERR_DIV0 = -1 };

static int divide(int a, int b, int *out) {
    if (b == 0) return ERR_DIV0;
    *out = a / b;
    return OK;
}

int main(void) {
    int r;

    if (divide(10, 0, &r) != OK) printf("error: division by zero\n");
    if (divide(10, 2, &r) == OK) printf("result = %d\n", r);
    return 0;
}

How It Works

  1. The function checks the divisor before dividing.
  2. The caller checks the return code.
  3. On success the quotient is written through out.

Edge Cases & Pitfalls

Check these before relying on Error Handling in real programs.

ignore

Ignoring return values

Unchecked failures turn into later crashes. Use __attribute__((warn_unused_result)) or review carefully.

mix

Mixing error styles

Pick a convention per project, such as 0 success and negative errors, and document it.

Key Takeaways

  • Check: every fallible call.
  • Convention: one style.
  • Cleanup: single exit.

One line: C has no exceptions.

Frequently Asked Questions

By returning status codes, setting errno, using output parameters, and checking those results at every call site.
On error, jump to a label near the end of the function that frees resources in reverse order, so cleanup code is written once.
Continue with "C errno and perror()" next. Read errno, print readable messages with perror and strerror. The C Advanced Concepts hub lists every topic in order.

Next: errno and perror()

Read errno, print readable messages with perror and strerror.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful