Standard Library

The C Standard Library is a collection of pre-written functions, macros, and types that provide a wide range of functionality to C programs. It’s defined by the C standard (ISO/IEC 9899) and is available as part of the C programming language. The Standard Library includes functions for performing various operations, such as input/output, memory management, string manipulation, mathematical calculations, and more.

The C Standard Library is crucial because it allows developers to write code more efficiently by utilizing these pre-built functions and facilities rather than reimplementing common tasks. It also promotes portability of C code across different systems and platforms.

Recall that external references may be resolved after compilation, during link time. Typically, on a modern system, the standard library is implemented as a monolithic dynamic library, and programs which use the symbols it exposes are linked to it at run-time. The actual interface of the library is organized into 24 headers:

Functionality

Header File

Diagnostics

assert.h

Complex Math

complex.h

Character handling

ctype.h

Errors

errno.h

Floating-point environment

fenv.h

Characteristics of floating types

float.h

Format conversion of integer types

inttypes.h

Alternative spellings

iso646.h

Sizes of integer types

limits.h

Localization

locale.h

Mathematics

math.h

Nonlocal jumps

setjmp.h

Signal handling

signal.h

Variable arguments

stdarg.h

Boolean type and values

stdbool.h

Common definitions

stddef.h

Integer types

stdint.h

Input/output

stdio.h

General utilities

stdlib.h

String handling

string.h

Type-generic math

tgmath.h

Date and time

time.h

Extended multibyte/wide character utilities

wchar.h

Wide character classification and mapping utilities

wctype.h

Most of these components are fairly simple and only provide a few features. For example, the stdbool.h header exposes macro definitions for bool, true, and false, as well as the __bool_true_false_are_defined macro. The entire stdbool.h header is essentially,

stdbool.h
#ifndef __bool_true_false_are_defined
#define bool _Bool
#define true 1
#define false 0
#define __bool_true_false_are_defined 1
#endif

Others are highly specific to particular applications, such as the floating, complex, and regular math facilities. The following headers are the most commonly used and encountered in general systems programming contexts.

Core

The standard library, stdlib.h, header provides several utilities which cover:

  • Numeric conversion functions

  • Random number generation

  • Memory management

  • Communication with the external environment

  • Searching and sorting

  • Integer arithmetic

  • Multi-byte (wide) character functions

The standard definitions, stddef.h, header provides several critical common definitions. It defines the types ptrdiff_t–the signed integer type which represents the result of subtracting two pointers, size_t–the unsigned integer type that can represent the size of any object, and wchar_t–an integer type that can represent any character in any supported locale, called a “wide character”. It also defines the macro NULL, which is a null pointer constant, and offsetof(type, member), which gives the offset of a structure member within the overall structure, in bytes.

The standard integers, stdint.h, header provides several fixed-width integer types, in the form uintX_t, uint_leastX_t, uint_fastX_t, where X is 8, 16, 32, or 64. The first form is an exact width type of X bits, while the second and third forms are the smallest and fastest types, respectively, that are at least X bits wide. Additionally, the intptr_t and uintptr_t types are provided such that a void* can be converted to, and then back, to either type, and still point to the same object. Finally, intmax_t and uintmax_t are provided, each corresponding to the largest signed and unsigned integer types, respectively. Note that all of these provided types are type aliases corresponding to one of the types described earlier

Diagnostics and Debugging

The assert.h header provides the debug-build assert(...) macro, which can be used for run-time assertion of certain conditions. If the NDEBUG macro is set, the assert(...) macro does nothing, so assertions can be easily disabled in release builds by adding a -DNDEBUG compilation flag, as we will see later on.

The errno.h header provides the errno (error number) macro, which expands to a modifiable int lvalue, along with error code macros which expand to distinct positive integer values. When library functions encounter errors, they set the value of errno to indicate the error that occurred. The library only requires three error code macros, EDOM, EILSEQ, and ERANGE, but many others are typically provided by an implementation that correspond to the types of errors that might be encountered in that particular execution environment.

Working with Text

The ctype.h character type header provides a portable interface for querying various properties of individual characters, such as isalnum, isalpha, isblank, and so on. Additionally, it provides a portable mechanism for converting characters to their upper-and-lower-case counterparts, via the toupper and tolower functions.

The string.h string manipulation header provides several facilities for working with strings, which are a ubiquitous data type in C. This header provides mechanisms for copying, concatenating, comparing, and searching strings. It also provides the memset function for setting a region of memory to a particular value, the strerror function for converting an errno value to a human-readable string, and the strlen function for calculating the length of a string.

Formatted Input/Output

The standard i/o, stdio.h, header provides a portable interface for manipulating and accessing files, reading and writing formatted data, individual characters, and unformatted data. It also has facilities for handling i/o errors, and printing diagnostic messages related to errno.

POSIX extensions

POSIX specifies several additional headers for interacting with POSIX systems portably–but which are not portable to non-POSIX systems such as Windows. It also extends several of the standard headers, such as providing a large number of additional error codes in the errno.h header. Many of these additional headers are application specific, with the unistd.h header acting as the POSIX counterpart to stddef.h by providing several common definitions and utilities.