C standard library

From HandWiki
Revision as of 04:39, 9 March 2024 by Gametune (talk | contribs) (fixing)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Short description: Standard library for the C programming language

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)

Header files

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:

Name From Description
<assert.h> Declares the assert macro, used to assist with detecting logical errors and other types of bugs while debugging a program.
<complex.h> C99 Defines a set of functions for manipulating complex numbers.
<ctype.h> Defines set of functions used to classify characters by their types or to convert between upper and lower case in a way that is independent of the used character set (typically ASCII or one of its extensions, although implementations utilizing EBCDIC are also known).
<errno.h> For testing error codes reported by library functions.
<fenv.h> C99 Defines a set of functions for controlling floating-point environment.
<float.h> Defines macro constants specifying the implementation-specific properties of the floating-point library.
<inttypes.h> C99 Defines exact-width integer types.
<iso646.h> NA1 Defines several macros that implement alternative ways to express several standard tokens. For programming in ISO 646 variant character sets.
<limits.h> Defines macro constants specifying the implementation-specific properties of the integer types.
<locale.h> Defines localization functions.
<math.h> Defines common mathematical functions.
<setjmp.h> Declares the macros setjmp and longjmp, which are used for non-local exits.
<signal.h> Defines signal-handling functions.
<stdalign.h> C11 For querying and specifying the alignment of objects.
<stdarg.h> For accessing a varying number of arguments passed to functions.
<stdatomic.h> C11 For atomic operations on data shared between threads.
<stdbool.h> C99 Defines a Boolean data type.
<stddef.h> Defines several useful types and macros.
<stdint.h> C99 Defines exact-width integer types.
<stdio.h> Defines core input and output functions
<stdlib.h> Defines numeric conversion functions, pseudo-random numbers generation functions, memory allocation, process control functions
<stdnoreturn.h> C11 For specifying non-returning functions
<string.h> Defines string-handling functions
<tgmath.h> C99 Defines type-generic mathematical functions.
<threads.h> C11 Defines functions for managing multiple threads, mutexes and condition variables
<time.h> Defines date- and time-handling functions
<uchar.h> C11 Types and functions for manipulating Unicode characters
<wchar.h> NA1 Defines wide-string-handling functions
<wctype.h> NA1 Defines set of functions used to classify wide characters by their types or to convert between upper and lower case

Three of the header files (complex.h, stdatomic.h, and threads.h) are conditional features that implementations are not required to support.

The POSIX standard added several nonstandard C headers for Unix-specific functionality. Many have found their way to other architectures. Examples include fcntl.h and unistd.h. A number of other groups are using other nonstandard headers – the GNU C Library has alloca.h, and OpenVMS has the va_count() function.

Documentation

On Unix-like systems, the authoritative documentation of the API is provided in the form of man pages. On most systems, man pages on standard library functions are in section 3; section 7 may contain some more generic pages on underlying concepts (e.g. man 7 math_error in Linux).

Implementations

Unix-like systems typically have a C library in shared library form, but the header files (and compiler toolchain) may be absent from an installation so C development may not be possible. The C library is considered part of the operating system on Unix-like systems; in addition to functions specified by the C standard, it includes other functions that are part of the operating system API, such as functions specified in the POSIX standard. The C library functions, including the ISO C standard ones, are widely used by programs, and are regarded as if they were not only an implementation of something in the C language, but also de facto part of the operating system interface. Unix-like operating systems generally cannot function if the C library is erased. This is true for applications which are dynamically as opposed to statically linked. Further, the kernel itself (at least in the case of Linux) operates independently of any libraries.

On Microsoft Windows, the core system dynamic libraries (DLLs) provide an implementation of the C standard library for the Microsoft Visual C++ compiler v6.0; the C standard library for newer versions of the Microsoft Visual C++ compiler is provided by each compiler individually, as well as redistributable packages. Compiled applications written in C are either statically linked with a C library, or linked to a dynamic version of the library that is shipped with these applications, rather than relied upon to be present on the targeted systems. Functions in a compiler's C library are not regarded as interfaces to Microsoft Windows.

Many C library implementations exist, provided with both various operating systems and C compilers. Some of the popular implementations are the following:

  • The BSD libc, various implementations distributed with BSD-derived operating systems
  • GNU C Library (glibc), used in GNU Hurd, GNU/kFreeBSD and Linux
  • Microsoft C run-time 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].
  • dietlibc, an alternative small implementation of the C standard library (MMU-less)
  • μClibc, a C standard library for embedded μClinux systems (MMU-less)
  • Newlib, a C standard library for embedded systems (MMU-less)[5] and used in the Cygwin GNU distribution for Windows
  • klibc, primarily for booting Linux systems
  • musl, another lightweight C standard library implementation for Linux systems[6]
  • Bionic, originally developed by Google for the Android embedded system operating system, derived from BSD libc
  • picolibc, developed by Keith Packard, targeting small embedded systems with limited RAM, based on code from Newlib and AVR Libc

Compiler built-in functions

Some compilers (for example, GCC[7]) provide built-in versions of many of the functions in the C standard library; that is, the implementations of the functions are written into the compiled object file, and the program calls the built-in versions instead of the functions in the C library shared object file. This reduces function-call overhead, especially if function calls are replaced with inline variants, and allows other forms of optimization (as the compiler knows the control-flow characteristics of the built-in variants), but may cause confusion when debugging (for example, the built-in versions cannot be replaced with instrumented variants).

However, the built-in functions must behave like ordinary functions in accordance with ISO C. The main implication is that the program must be able to create a pointer to these functions by taking their address, and invoke the function by means of that pointer. If two pointers to the same function are derived in two different translation units in the program, these two pointers must compare equal; that is, the address comes by resolving the name of the function, which has external (program-wide) linkage.

Linking, libm

Under FreeBSD[8] and glibc,[9] some functions such as sin() are not linked in by default and are instead bundled in the mathematical library libm. If any of them are used, the linker must be given the directive -lm. POSIX requires that the c99 compiler supports -lm, and that the functions declared in the headers math.h, complex.h, and fenv.h are available for linking if -lm is specified, but does not specify if the functions are linked by default.[10] musl satisfies this requirement by putting everything into a single libc library and providing an empty libm.[11]

Detection

According to the C standard the macro __STDC_HOSTED__ shall be defined to 1 if the implementation is hosted. A hosted implementation has all the headers specified by the C standard. An implementation can also be freestanding which means that these headers will not be present. If an implementation is freestanding, it shall define __STDC_HOSTED__ to 0.

Problems and workarounds

Buffer overflow vulnerabilities

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

  • string-manipulation routines, including strcpy() and strcat(), for lack of bounds checking and possible buffer overflows if the bounds are not checked manually;
  • string routines in general, for side-effects, encouraging irresponsible buffer usage, not always guaranteeing valid null-terminated output, linear length calculation;[lower-alpha 2]
  • printf() family of routines, for spoiling the execution stack when the format string does not match the arguments given. This fundamental flaw created an entire class of attacks: format string attacks;
  • gets() and scanf() family of I/O routines, for lack of (either any or easy) input length checking.

Except the extreme case with gets(), all the security vulnerabilities can be avoided by introducing auxiliary code to perform memory management, bounds checking, input checking, etc. This is often done in the form of wrappers that make standard library functions safer and easier to use. This dates back to as early as The Practice of Programming book by B. Kernighan and R. Pike where the authors commonly use wrappers that print error messages and quit the program if an error occurs.

The ISO C committee published Technical reports TR 24731-1[12] and is working on TR 24731-2[13] to propose adoption of some functions with bounds checking and automatic buffer allocation, correspondingly. The former has met severe criticism with some praise,[14][15] the latter received mixed responses. Despite this, TR 24731-1 has been implemented into Microsoft's C standard library and its compiler issues warnings when using old "insecure" functions.

Threading problems, vulnerability to race conditions

The strerror() routine is criticized for being thread unsafe and otherwise vulnerable to race conditions.

Error handling

The error handling of the functions in the C standard library is not consistent and sometimes confusing. According to the Linux manual page math_error, "The current (version 2.8) situation under glibc is messy. Most (but not all) functions raise exceptions on errors. Some also set errno. A few functions set errno, but do not raise an exception. A very few functions do neither."[16]

Standardization

The original C language provided no built-in functions such as I/O operations, unlike traditional languages such as COBOL and Fortran.[citation needed] Over time, user communities of C shared ideas and implementations of what is now called C standard libraries. Many of these ideas were incorporated eventually into the definition of the standardized C language.

Both Unix and C were created at AT&T's Bell Laboratories in the late 1960s and early 1970s. During the 1970s the C language became increasingly popular. Many universities and organizations began creating their own variants of the language for their own projects. By the beginning of the 1980s compatibility problems between the various C implementations became apparent. In 1983 the American National Standards Institute (ANSI) formed a committee to establish a standard specification of C known as "ANSI C". This work culminated in the creation of the so-called C89 standard in 1989. Part of the resulting standard was a set of software libraries called the ANSI C standard library.

POSIX standard library

POSIX, as well as SUS, specify a number of routines that should be available over and above those in the basic C standard library. The POSIX specification includes header files for, among other uses, multi-threading, networking, and regular expressions. These are often implemented alongside the C standard library functionality, with varying degrees of closeness. For example, glibc implements functions such as fork within libc.so, but before NPTL was merged into glibc it constituted a separate library with its own linker flag argument. Often, this POSIX-specified functionality will be regarded as part of the library; the basic C library may be identified as the ANSI or ISO C library.

BSD libc

BSD libc is a superset of the POSIX standard library supported by the C libraries included with BSD operating systems such as FreeBSD, NetBSD, OpenBSD and macOS. BSD libc has some extensions that are not defined in the original standard, many of which first appeared in 1994's 4.4BSD release (the first to be largely developed after the first standard was issued in 1989). Some of the extensions of BSD libc are:

  • sys/tree.h – contains an implementation of red–black tree and splay tree[17][18]
  • sys/queue.h – implementations of Linked list, queues, tail queue, etc.[19][20]
  • fgetln() – defined in stdio.h. This can be used to read a file line by line.[21][22][23]
  • fts.h – contains some functions to traverse a file hierarchy[24][25]
  • db.h – some functions to connect to the Berkeley DB[26][27]
  • strlcat() and strlcpy() – secure alternatives for strncat() and strncpy()[28][29][30][31][32]
  • err.h – contains some functions to print formatted error messages[33][34]
  • vis.h – contains the vis() function. This function is used to display non-printable characters in a visual format.[35][36][37]

The C standard library in other languages

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

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.

See also

Notes

  1. Morris worm that takes advantage of the well-known vulnerability in gets() have been created as early as in 1988.
  2. in C standard library, string length calculation and looking for a string's end have linear time complexities and are inefficient when used on the same or related strings repeatedly

References

  1. ISO/IEC (2018). ISO/IEC 9899:2018(E): Programming Languages - C §7
  2. "The GNU C Library – Introduction". http://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_chapter/libc_1.html. 
  3. "Difference between C standard library and C POSIX library". 2012. https://stackoverflow.com/questions/9376837/difference-bewteen-c-standard-library-and-c-posix-library. 
  4. "C Standards" (in en-US). Keil. http://www.keil.com/support/docs/1893.htm. 
  5. "Re: Does Newlib support mmu-less CPUs?". Cygwin.com. 23 March 2006. http://www.cygwin.com/ml/newlib/2006/msg00224.html. 
  6. "musl libc". Etalabs.net. http://www.etalabs.net/musl/. 
  7. Other built-in functions provided by GCC, GCC Manual
  8. "Compiling with cc". http://www.freebsd.org/doc/en_US.ISO8859-1/books/developers-handbook/tools-compiling.html. 
  9. Weimer, Florian. "c - What functions is the libm intended for?". https://stackoverflow.com/questions/54054925/what-functions-is-the-libm-intended-for. 
  10. "c99 - compile standard C programs". The Open Group. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/c99.html. 
  11. "musl FAQ". https://www.musl-libc.org/faq.html. 
  12. "ISO/IEC TR 24731-1: Extensions to the C Library, Part I: Bounds-checking interfaces". open-std.org. 2007-03-28. http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1225.pdf. 
  13. "ISO/IEC WDTR 24731-2: Extensions to the C Library, Part II: Dynamic Allocation Functions". open-std.org. 2008-08-10. http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1337.pdf. 
  14. Do you use the TR 24731 'safe' functions in your C code? - Stack overflow
  15. "Austin Group Review of ISO/IEC WDTR 24731". http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1106.txt. 
  16. "math_error - detecting errors from mathematical functions". 2008-08-11. http://man7.org/linux/man-pages/man7/math_error.7.html. 
  17. "tree". 2007-12-27. http://man.freebsd.org/tree. 
  18. "Super User's BSD Cross Reference: /OpenBSD/sys/sys/tree.h". http://bxr.su/o/sys/sys/tree.h. 
  19. "queue". 2011-05-13. http://man.freebsd.org/queue. 
  20. "Super User's BSD Cross Reference: /OpenBSD/sys/sys/queue.h". http://bxr.su/o/sys/sys/queue.h. 
  21. "fgetln". 1994-04-19. http://man.freebsd.org/fgetln. 
  22. "Super User's BSD Cross Reference: /OpenBSD/lib/libc/stdio/fgetln.c". http://bxr.su/o/lib/libc/stdio/fgetln.c#fgetln. 
  23. "Super User's BSD Cross Reference: /OpenBSD/include/stdio.h". http://bxr.su/o/include/stdio.h. 
  24. "fts". 2012-03-18. http://man.freebsd.org/fts. 
  25. "Super User's BSD Cross Reference: /OpenBSD/include/fts.h". http://bxr.su/o/include/fts.h. 
  26. "db". 2010-09-10. http://man.freebsd.org/db. 
  27. "Super User's BSD Cross Reference: /OpenBSD/include/db.h". http://bxr.su/o/include/db.h. 
  28. Miller, Todd C. and Theo de Raadt. strlcpy and strlcat - consistent, safe, string copy and concatenation. Proceedings of the 1999 USENIX Annual Technical Conference, June 6–11, 1999, pp. 175–178.
  29. "Super User's BSD Cross Reference: /OpenBSD/lib/libc/string/strlcat.c". http://bxr.su/o/lib/libc/string/strlcat.c. 
  30. "Super User's BSD Cross Reference: /OpenBSD/lib/libc/string/strlcpy.c". http://bxr.su/o/lib/libc/string/strlcpy.c. 
  31. "Super User's BSD Cross Reference: /OpenBSD/lib/libc/string/strncat.c". http://bxr.su/o/lib/libc/string/strncat.c. 
  32. "Super User's BSD Cross Reference: /OpenBSD/lib/libc/string/strncpy.c". http://bxr.su/o/lib/libc/string/strncpy.c. 
  33. "err". 2012-03-29. http://man.freebsd.org/err. 
  34. "Super User's BSD Cross Reference: /OpenBSD/include/err.h". http://bxr.su/o/include/err.h. 
  35. "vis(3)". http://www.freebsd.org/cgi/man.cgi?query=vis&sektion=3&apropos=0&manpath=FreeBSD+9.1-RELEASE. 
  36. "Super User's BSD Cross Reference: /OpenBSD/lib/libc/gen/vis.c". http://bxr.su/o/lib/libc/gen/vis.c#vis. 
  37. "Super User's BSD Cross Reference: /OpenBSD/include/vis.h". http://bxr.su/o/include/vis.h. 
  38. "The Python Standard Library: 6.9. File Objects". Docs.python.org. https://docs.python.org/library/stdtypes.html#bltin-file-objects. 
  39. "libc". Rust Crates. https://doc.rust-lang.org/nightly/libc/index.html. 

Further reading

  • Plauger, P.J. (1992). The Standard C library (1 ed.). Prentice Hall. ISBN 978-0131315099. 

External links