The C and C Standard Libraries

The C and C++ Standard Libraries

Neither C nor C++ have keywords that perform I/O, manipulate strings, perform various mathematical computations, or a number of other useful procedures. These things are accomplished by using a set of predefined library functions that are supplied with the compiler. There are two basic styles of libraries: the C function library, which is supplied with all C and C++ compilers, and the C++ class library, which applies only to C++. Both libraries are summarized later in this guide.

Before your program can use a library function, it must include the appropriate header. In general, headers are usually files, but they are not necessarily files. It is permissible for a compiler to predefine the contents of a header internally. However, for all practical purposes, the standard C headers are contained in files that correspond to their names. The following table shows the standard headers defined by C89, along with those added by the 1995 Amendment 1.

Header

Supports

<assert.h>

The assert( ) macro

<ctype.h>

Character handling

<errno.h>

Error reporting

<float.h>

Implementation-dependent floating-point limits

<iso646.h>

Macros that correspond to various operators, such as && and ^; added in 1995 by Amendment 1

<limits.h>

Various implementation-dependent limits

<locale.h>

Localization

<math.h>

Various definitions used by the math library

<setjmp.h>

Nonlocal jumps

<signal.h>

Signal values

<stdarg.h>

Variable-length argument lists

<stddef.h>

Commonly used constants

<stdio.h>

File I/O

<stdlib.h>

Miscellaneous declarations

<string.h>

String functions

<time.h>

System time and date functions

<wchar.h>

Multibyte and wide-character functions; added in 1995 by Amendment 1

<wctype.h>

Multibyte and wide-character classification functions; added in 1995 by Amendment 1

The following table shows those headers added by C99.

Header

Supports

<complex.h>

Complex arithmetic.

<fenv.h>

The floating-point status flags and other aspects of the floating-point environment.

<inttypes.h>

Standard, portable set of integer type names; also supports functions that handle greatest-width integers.

<stdbool.h>

Boolean data types and defines the macro bool, which helps with C++ compatibility.

<stdint.h>

Standard, portable set of integer type names. This file is included by <inttypes.h>.

<tgmath.h>

Type-generic floating-point macros.

For C++, headers are specified using standard header names, which do not end with .h. Thus, the C++ headers do not specify filenames. Instead, they are simply standard identifiers that the compiler can handle as it sees fit. This means that a header may be mapped to a filename, but this is not required. The C++ headers are shown here. Those associated either directly or indirectly with the Standard Template Library (STL) are indicated.

C++ Header

Supports

<algorithm>

Various operations on containers (STL)

<bitset>

Bitsets (STL)

<complex>

Complex numbers

<deque>

Double-ended queues (STL)

<exception>

Exception handling

<fstream>

Stream-based file I/O

<functional>

Various function objects (STL)

<iomanip>

I/O manipulators

<ios>

Low-level I/O classes

<iosfwd>

Forward declarations for I/O system

<iostream>

Standard I/O classes

<istream>

Input streams

<iterator>

Access to contents of containers (STL)

<limits>

Various implementation limits

<list>

Linear lists (STL)

<locale>

Localization-specific information

<map>

Maps (keys with values) (STL)

<memory>

Memory allocation via allocators (STL)

<new>

Memory allocation using new

<numeric>

General-purpose numeric operations (STL)

<ostream>

Output streams

<queue>

Queues (STL)

<set>

Sets (STL)

<sstream>

String streams

<stack>

Stacks (STL)

<stdexcept>

Standard exceptions

<streambuf>

Buffered streams

<string>

Standard string class (STL)

<typeinfo>

Runtime type information

<utility>

General purpose templates (STL)

<valarray>

Operations on arrays containing values

<vector>

Vectors (dynamic arrays) (STL)

C++ also defines the following headers that correspond to the C headers (except those added by C99).

<cassert>

<cctype>

<cerrno>

<cfloat>

<ciso646>

<climits>

<clocale>

<cmath>

<csetjmp>

<csignal>

<cstdarg>

<cstddef>

<cstdio>

<cstdlib>

<cstring>

<ctime>

<cwchar>

<cwctype>

In standard C++, all of the information relating to the standard library is defined under the std namespace. Thus, to gain direct access to these items, you will need to include the following using statement after including the necessary headers:

using namespace std; 

Alternatively, you can qualify each library identifier with std::, such as std::cout, rather than bringing the entire library into the global namespace. However, qualifying each name can get to be tedious.

Programming Tip 

If you are using an older C++ compiler, then it may not support the modern-style C++ headers or the namespace command. If this is the case, then you will need to use the older, traditional-style headers. These use the same names as the modern headers but include the .h (thus, they resemble C headers). For example, the following includes <iostream> using the traditional approach:

#include <iostream.h> 

When using the traditional-style header, all of the names defined by the header are placed in the global namespace, not the one defined by std. Thus, no using statement is required.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net