The Standard Library
Using the Library
The C89 standard library is divided into 15 parts, with
each part described by a header. C99 has an additional nine headers.Most
compilers come with more extensive library. so we can
t count on them to be available with other compilers. These headers often
provide functions that are specific on a particular computer or operating
system (which explains why they're not standard). They may provide functions
that allow more control over the screen and keyboard. Headers that support
graphics or a window-based user interface are also common.
The
standard headers consist primarily of function prototypes, type definitions,
and macro definitions. If one of our files contains a call of a function
declared in a header or uses one of the types or macros defined there, we'll
need to include the header at the beginning of the file. When a file includes
several standard headers, the order of #include directives doesn't matter.
It's also legal to include a standard header more than once.
Restrictions on Names Used in the
Library
Any file that includes a standard header must obey a couple
of rules. First, it can't use the names of macros defined in that header for
any other purpose. If a file includes , for example, it
can't reuse NULL, since a macro by that name is already defined in
. Second, library names with file scope (typedef
names, in particular) can't be redefined at the file level. Thus, if a file
includes , it can't define size_t as a identifier
with file scope, since defines size_t to be a
typedef name.
Although
these restrictions are pretty obvious, C has other restrictions that you might
not expect:
Identifiers that begin with an underscore followed by an
upper-case letter or a second underscore are reserved for use within the
library: programs should never use names of this form for any purpose.
Identifiers that begin with an underscore are reserved for
use as identifiers and tags with file scope. You should never use such a name
for your own purposes unless it's declared inside a function.
Every identifier with external linkage in the standard
library is reserved for use as an identifier with external linkage. In
particular, the names of all standard library functions are reserved. Thus,
even if a file doesn't include , it shouldn't define an
external function named printf, since there's already a function with this
name in the library.
These rules apply to every file in a program,
regardless of which headers the file includes. Although these rules aren't
always enforced, failing to obey them can lead to a program that's not
portable.
The
rules listed above apply not just to names that are currently used in the
library, but also to names that are set aside for future use. The complete
description of which names are reserved is rather lengthy: you'll find it in
the C standard under "future library directions." As an example, C
reserves identifiers that begin with str followed by a lower-case
letter, so that functions with such names can be added to the header.
Functions
Hidden by Macros
It's
common for C programmers to replace small functions by parameterized macros.
This practice occurs even in the standard library. The C standard allows headers
to define macros that have the same names as library functions, but protects
the programmer by requiring that a true function be available as well. As a
result, it's not unusual for a library header to declare a function and
define a macro with the same name.
We've
already seen an example of a macro duplicating a library function, getchar is a
library function declared in the header. It has the following
prototype:
int
getchar(void);
#define getchar() getc(stdin)
By default, a call of getchar will
be treated as a macro invocation (since macro names are replaced during
preprocessing).
Most of the time, we're happy using
a macro instead of a true function, because it will probably make our program
run faster. Occasionally, though, we want a genuine function, perhaps to
minimize the size of the executable code.
C89
Library Overview
We'll now
take a quick look at the headers in the C89 standard library. This section can
serve as a "road map" to help you determine which part of the library
you need. Each header is described in detail.
Contains only the
assert macro, which allows us to insert self-checks into a program. If any
check fails, the program terminates.
< ctype.h
> Character Handling
cctype.
h> header >
Provides functions for classifying
characters and for converting letters from lower to upper case or vice versa.
< errno.h>
Errors
Provides macros that describe the
characteristics of floating types, including their range and accuracy.
Provides macros that describe the
characteristics of integer types (including character types), including their
maximum and minimum values.
Provides
functions to help a program adapt its behavior to a country or other geographic
region. Locale-specific behavior includes the way numbers are printed (such as
the character used as the decimal point), the format of monetary values (the
currency symbol, for example), the character set, and the appearance of the
date and time.
Provides
common mathematical functions, including trigonometric, hyperbolic,
exponential, logarithmic, power, nearest integer, absolute value, and remainder
functions.
Provides
the set jmp and longjmp functions, set jmp "marks" a place in a program:
longjmp can then be used to return to that place later. These functions make it
possible to jump from one function into another, still-active function,
bypassing the normal function-return mechanism, set jmp and longjmp are used
primarily for handling serious problems that arise during program execution.
< signal.h >
Signal Handling
Provides
functions that deal with exceptional conditions (signals), including interrupts
and run-time errors. The signal function installs a function to be called if a
given signal should occur later. The raise function causes a signal to occur.
< stdarg. h>
Variable Arguments
Provides tools for writing functions that, like
printf and scanf, can have a variable number of arguments.
< stdde f. h> Common Definitions
Provides definitions of frequently used types and macros.
<stdio.h>
Input/Output
Provides a large assortment of input/output
functions, including operations on both sequential and random-access files.
< stdlib.h> General
Utilities
A
"catchall" header for functions that don't fit into any of the other
headers. The functions in this header can convert strings to numbers, generate
pseudo-random numbers, perform memory management tasks, communicate with the
operating system, do searching and sorting, and perform conversions between
multibyte characters and wide characters.
Provides
functions thai perform string operations, including copying, concatenation,
comparison, and searching, as well as functions that operate on arbitrary
blocks of memory.
<
time. h> Date and Time
Provides functions for determining
the time (and date), manipulating times, and formatting times for display.
<
complex.h> Complex
Arithmetic
Defines
the complex and I macros, which are useful when working with complex numbers.
Also provides functions for performing mathematical operations on complex
numbers.
< fenv. h>
Floating-Point Environment
Provides
access to floating-point status flags and control modes. For example, a program
might test a flag to see if overflow occurred during a floating-point operation
or set a control mode to specify how rounding should be done.
Defines
macros that can be used in format strings for input/output of the integer types
declared in . Also provides functions for working with greatest-width
integers.
Defines
macros that represent certain operators .These macros are useful for writing
programs in an environment where these characters might not be pan of the local
character set.
Defines the bool. true, and false macros, as well as a macro that can
be used to test whether these macros have been defined.
Declares integer types with specified widths and defines
related macros (such as macros that specify the maximum and minimum values of
each type). Also defines parameterized macros that construct integer constants
with specific types.
< tgmath.h > Type-Generic Math
In C99. there are multiple versions of many
math functions in the and headers. The
"type-generic" macros in can detect the types of the
arguments passed to them and substitute a call of the appropriate
or function.
Provides functions for wide-character input/output and wide string
manipulation.
The wide-character version of .
Provides functions for classifying and changing the case of wide characters.
No comments:
Post a Comment