Appendix B · The standard library.
Place headers, strings, memory, diagnostics, and time in a coherent library map.
The idea behind the syntax.
The C standard library supplies declared interfaces for operations that would otherwise be repeated in every program. stdio.h covers streams and formatted I/O, stdlib.h includes allocation, conversion, and process utilities, string.h covers byte strings and raw memory operations, and ctype.h classifies character values under its input rules. Other headers define integer limits, math operations, time, diagnostics, and variadic arguments.
A header gives declarations and macros; linking supplies the implementation. A function's name is not enough to use it correctly. Read its parameter types, return value, error signal, and preconditions. For example, strlen assumes a valid zero-terminated sequence, memcpy assumes enough non-overlapping accessible bytes, and malloc may return null. The library is powerful precisely because each operation has a narrow contract.
What C guarantees.
errno is a thread-local error indicator used by some library functions, but it is meaningful only when the function's contract says to inspect it, usually after setting it to zero before a conversion. A successful call need not clear a previous errno value. assert can document an internal invariant during debugging, but it may be disabled in a release configuration and must not carry required side effects.
Time functions, character classification, and math each have representation limits. time_t and calendar conversion depend on implementation range and time-zone context; ctype functions require an unsigned-char value or EOF; floating-point calculations may round or overflow. A reliable program builds a small adapter that checks these boundaries and returns domain-meaningful results. The example uses strtol's end pointer and range result to turn text into a validated value.
Why the detail matters.
The standard headers are a map of responsibilities, not a toolbox where any vaguely similar function will do. string.h functions operate on either counted bytes or zero-terminated byte strings depending on the function. stdio.h stream functions report counts, sentinels, or statuses. stdlib.h conversions may use an end pointer and errno. A reader should identify the specific contract before using an unfamiliar function, especially when the function receives a pointer whose accessible extent is not encoded in the type.
For diagnostics, preserve context without inventing success. A failed fopen can be reported with the path and a system error description when appropriate, while a failed parse can name the unexpected text and required format. Cleanup must still occur for resources acquired before the error. The library provides raw mechanisms; the program decides which failures are recoverable and what message will help the person or caller correct them.
See the rule in context.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
int main(void) {
const char text[] = "125";
char *end;
errno = 0;
long value = strtol(text, &end, 10);
if (errno == ERANGE || end == text || *end != '\0' || value > INT_MAX || value < INT_MIN) return 1;
printf("Parsed: %d\n", (int)value);
return 0;
}strtol parses 125 as a long and places end at the zero terminator. errno remains clear of ERANGE, digits were consumed, no trailing text remains, and 125 fits int. The cast is therefore justified, and the output is Parsed: 125. This sequence illustrates a library contract: the return value, end pointer, errno, and destination range each answer a different question.
Where the rule stops.
Do not inspect errno alone to decide whether a call failed. It can retain an earlier value; pair it with the called function's documented failure signal and set it appropriately before operations such as strtol.
Keep this model.
The library is not one uniform error-handling system. Some functions return a sentinel, some return a count, some set errno, and some require a separate status query. Reading the specific contract is part of learning C, not a detail to postpone until a bug appears.