Coding/Learn/c/c error handling and debugging
C Foundational Course
01.Introduction, Compilation Pipeline & Syntax
02.Variables, Primitives & Explicit Type Casting
03.Operators, Precedence & Booleans (<stdbool.h>)
04.Control Flow: If-Else, Ternary & Switch
05.Loops (For, While, Do-While) & Jump Statements
06.Contiguous Arrays & Multi-Dimensional Matrices
07.Null-Terminated Strings & <string.h>
08.User Input, scanf & fgets Buffer Safety
09.Pointers, Memory Addresses & Dereferencing
10.Functions, Prototypes & Pass-by-Pointer
11.11. Recursion & High-Speed Function Pointers
12.File Streams: Create, Write & Read (fopen/fclose)
13.Structures, Unions & typedef Aliasing
14.Memory Word Alignment & Struct Padding
15.Enums & Bitwise Manipulation (Flags & Masks)
16.Dynamic Memory: malloc, calloc, realloc & free
17.Defensive Error Handling, errno & Assertions
18.Preprocessor Macros & Storage Classes
19.Fixed-Width Types (stdint.h), Math & Time (<time.h>)
20.Capstone: High-Performance In-Memory Key-Value Engine
Course Certificate0%

Complete all lessons to earn

LearnDefensive Error Handling, errno & Assertions
17 / 20 0%
Lesson 17 of 20
6 mins read

Defensive Error Handling, errno & Assertions

PreviousNext Lesson
In Plain English

When standard library or OS system calls fail, they populate the global errno integer variable. Inspect human-readable descriptions via strerror(errno) or perror(). Use <assert.h> to enforce invariant preconditions during development.

The Mental Model

"errno is the error diagnostic readout on a car dashboard: a code like 2 means "No such file or directory" (fuel cap loose)."

Deep Dive: How It Works

errno: Thread-local integer updated by failed POSIX system calls.

strerror() & perror(): Translate integer error numbers into human-readable strings.

assert(): Enforces preconditions; compiled out in release builds when -DNDEBUG is set.

Syntax Blueprint

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

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

Include errno.h and format human-readable error strings with strerror.

PointCore Rules to Remember

Point star
Zero Overhead in Production: Assertions can be disabled in production releases using -DNDEBUG.
Point star
Explicit Return Codes: C functions typically return 0 or non-negative on success, and -1 or NULL on failure.
Point star
Thread Safety: In modern C, errno is thread-safe and isolated per thread.

Common Beginner Traps & How to Fix Them

Mistake:Checking errno without first checking if the function call actually returned a failure indicator.

Why it happens: errno is not reset to 0 by successful calls; reading it without a preceding failure gives stale errors.

How to fix: Only inspect errno if the function returned NULL or -1.

Pro Tip: Use assertions generously to validate that incoming pointer arguments are non-NULL before proceeding.

Live Interactive Example

Hit Run Code to see it live
Translate System Error Codes
C11 / GCC 9.2
1
2
3
4
5
6
7
8
9
Output Console
Click "Run Code" to view the rendered output.
How it works: strerror(2) translates standard POSIX ENOENT error code into human text.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Confirm Error Verification

  • PointPrint "Error Checked: TRUE\n" after strerror output.
1
2
3
4
5
6
7
8
9
10
11
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates