Katana VentraIP

C standard library

The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard.[1] Starting from the original ANSI C standard, it was developed at the same time as the C library POSIX specification, which is a superset of it.[2][3] Since ANSI C was adopted by the International Organization for Standardization,[4] the C standard library is also called the ISO C library.

The C standard library provides macros, type definitions and functions for tasks such as string handling, mathematical computations, input/output processing, memory management, and several other operating system services.

Application programming interface (API)[edit]

Header files[edit]

The application programming interface (API) of the C standard library is declared in a number of header files. Each header file contains one or more function declarations, data type definitions, and macros.


After a long period of stability, three new header files (iso646.h, wchar.h, and wctype.h) were added with Normative Addendum 1 (NA1), an addition to the C Standard ratified in 1995. Six more header files (complex.h, fenv.h, inttypes.h, stdbool.h, stdint.h, and tgmath.h) were added with C99, a revision to the C Standard published in 1999, and five more files (stdalign.h, stdatomic.h, stdnoreturn.h, threads.h, and uchar.h) with C11 in 2011. In total, there are now 29 header files:

The , various implementations distributed with BSD-derived operating systems

BSD libc

(glibc), used in GNU Hurd, GNU/kFreeBSD and Linux

GNU C Library

part of Microsoft Visual C++. There are two versions of the library: MSVCRT that was a redistributable till v12 / Visual Studio 2013 with low C99 compliance, and a new one UCRT (Universal C Run Time) that is part of Windows 10 and 11, so always present to link against, and is C99 compliant too [1].

Microsoft C run-time library

an alternative small implementation of the C standard library (MMU-less)

dietlibc

μClibc

uclibc-ng

a C standard library for embedded systems (MMU-less)[5] and used in the Cygwin GNU distribution for Windows

Newlib

primarily for booting Linux systems

klibc

another lightweight C standard library implementation for Linux systems[6]

musl

originally developed by Google for the Android embedded system operating system, derived from BSD libc

Bionic

developed by Keith Packard, targeting small embedded systems with limited RAM, based on code from Newlib and AVR Libc

picolibc

Problems and workarounds[edit]

Buffer overflow vulnerabilities[edit]

Some functions in the C standard library have been notorious for having buffer overflow vulnerabilities and generally encouraging buggy programming ever since their adoption.[a] The most criticized items are:

sys/tree.h – contains an implementation of and splay tree[17][18]

red–black tree

sys/queue.h – implementations of , queues, tail queue, etc.[19][20]

Linked list

fgetln() – defined in stdio.h. This can be used to read a file line by line.[22][23]

[21]

fts.h – contains some functions to traverse a file hierarchy[25]

[24]

db.h – some functions to connect to the [26][27]

Berkeley DB

strlcat() and strlcpy() – secure alternatives for strncat() and strncpy()[29][30][31][32]

[28]

err.h – contains some functions to print formatted error messages[34]

[33]

vis.h – contains the vis() function. This function is used to display non-printable characters in a visual format.[36][37]

[35]

The C standard library in other languages[edit]

Some languages include the functionality of the standard C library in their own libraries. The library may be adapted to better suit the language's structure, but the operational semantics are kept similar. The C++ language, for example, includes the functionality of the C standard library in the namespace std (e.g., std::printf, std::atoi, std::feof), in header files with similar names to the C ones (cstdio, cmath, cstdlib, etc.). Other languages that take similar approaches are D, Perl, Ruby and the main implementation of Python known as CPython. In Python 2, for example, the built-in file objects are defined as "implemented using C's stdio package",[38] so that the available operations (open, read, write, etc.) are expected to have the same behavior as the corresponding C functions. Rust has a crate called libc which allows several C functions, structs, and other type definitions to be used.[39]

Comparison to standard libraries of other languages[edit]

The C standard library is small compared to the standard libraries of some other languages. The C library provides a basic set of mathematical functions, string manipulation, type conversions, and file and console-based I/O. It does not include a standard set of "container types" like the C++ Standard Template Library, let alone the complete graphical user interface (GUI) toolkits, networking tools, and profusion of other functionality that Java and the .NET Framework provide as standard. The main advantage of the small standard library is that providing a working ISO C environment is much easier than it is with other languages, and consequently porting C to a new platform is comparatively easy.

C++ Standard Library

(1992). The Standard C library (1 ed.). Prentice Hall. ISBN 978-0131315099.

Plauger, P.J.

The C Library Reference Guide

Handy list of which headers are in which standard

Microsoft on MSDN

Universal C runtime routines by category

NetBSD Archived 2015-12-23 at the Wayback Machine and full C library source

C libraries manual

Manual pages for the original C standard libraries in Unix