C standard library
standard library for the C programming language, as specified in the ANSI C standard, providing macros, type definitions and functions for different tasks
≈ 8 min read
C standard library. encyclopegia.org, snapshot c-standard-library-035158-17c8a6cf19b02acb. /a/c-standard-library
Names in other languages
- Polskibiblioteka standardowa języka C
- PortuguêsBiblioteca padrão do C
- EnglishC standard library
- 中文C标准函数库
- 简体中文C标准函数库
- 繁體中文C標準函式庫
- 中文C標準函式庫
- 中文C标准函数库
- 中文C標準函式庫
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In the C++ programming language, the C++ Standard Library is a collection of classes and functions, which are written in the core language and part of the C++ ISO Standard itself.
The C standard library, sometimes referred to as libc, is the standard library for the C programming language specified in the ISO C standard. Starting from the original ANSI C standard, it was developed at the same time as the C POSIX library, which is a superset of it.
Since ANSI C was adopted by the International Organization for Standardization, the C standard library is also called the ISO C library. The C standard library provides and includes macros, type definitions and functions for tasks such as string manipulation, mathematical computation, input/output processing, memory management, and input/output.
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. An implementation of a version of the standard for the C language can be described as hosted or freestanding.
A hosted implementation supports the entire standard, and requires that all of the header files specified in the standard be present; these are typically implementations for programs running on an operating system. A freestanding implementation is not required to implement language features in the C library other than those specified in that version of the operating system as being required in freestanding implementations; these are typically implementations for programs running on a very limited operating system or directly on the hardware.
After a long period of stability, three new header files ( , , and ) were added with Normative Addendum 1 (NA1), an addition to the C Standard ratified in 1995. Six more header files ( , , , , , and ) were added with C99, a revision to the C Standard published in 19991, five more files ( , , , , and ) with C11 in 2011, two more files ( and ) with C23 in 2023, and two more files ( and ) in the upcoming C29, projected for 2029.
In total, there are now 33 header files: Legend: : Deprecated : Removed Three of the header files ( , , and ) 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 and . A number of other groups are using other nonstandard headers – the GNU C Library has , and OpenVMS has the va_count() function. The Microsoft C run-time library, which is part of Microsoft Visual C++.
There are two versions of the library: the formerly-redistributable (until Visual Studio 2013) MSVCRT which is not compliant to the C99 standard, and the newer UCRT (Universal C Run Time) shipped as part of Windows 10 and 11 which is C99-compliant * dietlibc, an alternative small implementation of the C standard library (MMU-less) μClibc, a C standard library for embedded μClinux systems (MMU-less) uclibc-ng, an embedded C library, fork of μClibc, still maintained, with memory management unit (MMU) support Newlib, a C standard library for embedded systems (MMU-less) 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 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 llvm-libc is a from-scratch implementation of the C standard library, built as part of the LLVM project
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. 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; 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 and is working on TR 24731-2 to propose adoption of some functions with bounds checking and automatic buffer allocation, correspondingly.
The former has met severe criticism with some praise, and the latter saw mixed response. Despite concerns, TR 24731-1 was integrated into the C standards track in ISO/IEC 9899:2011 (C11), Annex K (Bounds-checking interfaces), and implemented approximately in Microsoft's C/++ runtime (CRT) library for the Win32 and Win64 platforms.
(By default, Microsoft Visual Studio's C and C++ compilers issue warnings when using older, "insecure" functions. However, Microsoft's implementation of TR 24731-1 is subtly incompatible with both TR 24731-1 and Annex K, so it's common for portable projects to disable or ignore these warnings.
They can be disabled directly by issuing #pragma warning(disable : 4996) before/around the call site[s in question, or indirectly by issuing #define _CRT_SECURE_NO_WARNINGS 1 before including any headers. Command-line option /D_CRT_NO_SECURE_WARNINGS=1 should have the same effect as this #define .)
For backwards-/cross-compatibility to C and pre-Standard C++, functions can be accessed in the global namespace ( :: ), after ing the C standard header name as in C. However, the :: used for indicating the global namespace is necessary only when within the scope of other namespaces where the same symbol name is defined. Thus, the C++98 program include int main(void) { return puts("Hello, world!") == EOF; } should exhibit (apparently-)identical behavior to a code-identical C95 program.
From C++98 on, C functions are also made available in namespace std (e.g., C printf as C++ std::printf , atoi as std::atoi , feof as std::feof ), by including header instead of corresponding C header . E.g., substitutes for and for ; note lack of .h extension on C++ header names.
Thus, an equivalent (generally preferable) C++≥98 program to the above two is: include int main() { return std::puts("Hello, world") == EOF; } If using C++20 or later, can be substituted for (but not , as modules do not export macros, such as EOF ). A declaration above or within main can be issued to automatically qualify the std namespace (essentially applying the std:: prefix automatically), although writing a using statement can pollute the global namespace with potentially unwanted symbols if used in the global scope of a header.
A few of the C++≥98 versions of C's headers are missing; e.g., C≥11 and have no C++ counterparts. Others are reduced to placeholders, such as (until C++20) for C95 , all of whose requisite macros are rendered as keywords in C++98. C-specific syntactic constructs are not generally supported, even if their header is.
Several C headers exist primarily for C++ compatibility, and these tend to be near-empty in C++. For example, C99–17 require only #define bool _Bool define false 0 define true 1 define __bool_true_false_are_defined 1 in order to feign support for the C++98 bool , false , and true keywords in C. C++11 requires and for compatibility, but they need only define __bool_true_false_are_defined .
C23 deprecates older _Bool keyword in favor of new, C++98-equivalent bool , false , and true keywords, so the C≥23 and C++≥11 / cstdbool> headers are fully equivalent. (In particular, C23 does not require any __STDC_VERSION_BOOL_H__ macro for .) Access to C library functions via namespace and the C++≥98 header names is preferred where possible.
To encourage adoption, C++98 obsoletes the C ) header names, so it is possible that use of C compatibility headers will cause an especially strict C++98–20 preprocessor to raise a diagnostic of some sort. However, C++23 (unusually) de- obsoletes these headers, so newer C++ implementations/modes should not complain without being asked to specifically.
The C++≥23 standard library module std.compat puts all C standard library symbols in the global namespace (akin to including all headers), while the std module leaves C standard library symbols in the std namespace (akin to including the C++ versions of C headers). Other languages take a similar approach, placing C compatibility functions/routines under a common namespace; these include D, Perl, and Ruby.
Text derived from https://en.wikipedia.org/w/index.php?oldid=1377178737 under CC BY-SA 4.0; modifications have been made.
References
- ↩Wikipedia “C standard library” publication date (×1) · confidence 0.95 revision URL