This appendix is a summary of the library defined by the ANSI standard. The standard library is not part of the C language proper, but an environment that supports standard C will provide the function declarations and type and macro definitions of this library. We have omitted a few functions that are of limited utility or easily synthesized from others; we have omitted multi-byte characters; and we have omitted discussion of locale issues; that is, properties that depend on local language, nationality, or culture.
The functions, types and macros of the standard library are declared in standard headers:
A header can be accessed by
#include <<em>header>
Headers may be included in any order and any number of times. A header must be included outside of any external declaration or definition and before any use of anything it declares. A header need not be a source file.
External identifiers that begin with an underscore are reserved for use by the library, as are all other identifiers that begin with an underscore and an upper-case letter or another underscore.
The input and output functions, types, and macros defined in represent nearly one third of the library.
A stream is a source or destination of data that may be associated with a disk or other peripheral. The library supports text streams and binary streams, although on some systems, notably UNIX, these are identical. A text stream is a sequence of lines; each line has zero or more characters and is terminated by '\n'. An environment may need to convert a text stream to or from some other representation (such as mapping '\n' to carriage return and linefeed). A binary stream is a sequence of unprocessed bytes that record internal data, with the property that if it is written, then read back on the same system, it will compare equal.
A stream is connected to a file or device by openingit; the connection is broken by closing the stream. Opening a file returns a pointer to an object of type FILE, which records whatever information is necessary to control the stream. We will use ``file pointer'' and ``stream'' interchangeably when there is no ambiguity.
When a program begins execution, the three streams stdin, stdout, and stderr are already open.
The following functions deal with operations on files. The type size_t is the unsigned integral type produced by the sizeof operator.
The printf functions provide formatted output conversion.
int fprintf(FILE *stream, const char *format, ...)
fprintf converts and writes output to stream under the control of format. The return value is the number of characters written, or negative if an error occurred.
The format string contains two types of objects: ordinary characters, which are copied to the output stream, and conversion specifications, each of which causes conversion and printing of the next successive argument to fprintf. Each conversion specification begins with the character % and ends with a conversion character. Between the % and the conversion character there may be, in order:
Width or precision or both may be specified as *, in which case the value is computed by converting the next argument(s), which must be int.
The conversion characters and their meanings are shown in Table B.1. If the character after the % is not a conversion character, the behavior is undefined.
Table B.1 Printf Conversions
| Character | Argument type; Printed As |
|---|---|
| d,i | int; signed decimal notation. |
| o | int; unsigned octal notation (without a leading zero). |
| x,X | unsigned int; unsigned hexadecimal notation (without a leading 0x or 0X), using abcdef for 0x or ABCDEF for 0X. |
| u | int; unsigned decimal notation. |
| c | int; single character, after conversion to unsigned char |
| s | char *; characters from the string are printed until a '\0' is reached or until the number of characters indicated by the precision have been printed. |
| f | double; decimal notation of the form [-]mmm.ddd, where the number of d's is given by the precision. The default precision is 6; a precision of 0 suppresses the decimal point. |
| e,E | double; decimal notation of the form [-]m.dddddde+/-xx or [-]m.ddddddE+/-xx, where the number of d's is specified by the precision. The default precision is 6; a precision of 0 suppresses the decimal point. |
| g,G | double; %e or %E is used if the exponent is less than -4 or greater than or equal to the precision; otherwise %f is used. Trailing zeros and a trailing decimal point are not printed. |
| p | void *; print as a pointer (implementation-dependent representation). |
| n | int *; the number of characters written so far by this call to printf is written into the argument. No argument is converted. |
| % | no argument is converted; print a % |
The scanf function deals with formatted input conversion.
int fscanf(FILE *stream, const char *format, ...)
fscanf reads from stream under control of format, and assigns converted values through subsequent arguments, each of which must be a pointer. It returns when format is exhausted. fscanf returns EOF if end of file or an error occurs before any conversion; otherwise it returns the number of input items converted and assigned.
The format string usually contains conversion specifications, which are used to direct interpretation of input. The format string may contain:
A conversion specification determines the conversion of the next input field. Normally the result is placed in the variable pointed to by the corresponding argument. If assignment suppression is indicated by *, as in %*s, however, the input field is simply skipped; no assignment is made. An input field is defined as a string of non-white space characters; it extends either to the next white space character or until the field width, if specified, is exhausted. This implies that scanf will read across line boundaries to find its input, since newlines are white space. (White space characters are blank, tab, newline, carriage return, vertical tab, and formfeed.)
The conversion character indicates the interpretation of the input field. The corresponding argument must be a pointer. The legal conversion characters are shown in Table B.2.
The conversion characters d, i, n, o, u, and x may be preceded by h if the argument is a pointer to short rather than int, or by l (letter ell) if the argument is a pointer to long. The conversion characters e, f, and g may be preceded by l if a pointer to double rather than float is in the argument list, and by L if a pointer to a long double.
Table B.2 Scanf Conversions
| Character | Input Data; Argument type |
|---|---|
| d | decimal integer; int* |
| i | integer; int*. The integer may be in octal (leading 0) or hexadecimal (leading 0x or 0X). |
| o | octal integer (with or without leading zero); int *. |
| u | unsigned decimal integer; unsigned int *. |
| x | hexadecimal integer (with or without leading 0x or 0X); int*. |
| c | characters; char*. The next input characters are placed in the indicated array, up to the number given by the width field; the default is 1. No '\0' is added. The normal skip over white space characters is suppressed in this case; to read the next non-white space character, use %1s. |
| s | string of non-white space characters (not quoted); char *, pointing to an array of characters large enough to hold the string and a terminating '\0' that will be added. |
| e,f,g | floating-point number; float *. The input format for float's is an optional sign, a string of numbers possibly containing a decimal point, and an optional exponent field containing an E or e followed by a possibly signed integer. |
| p | pointer value as printed by printf("%p");, void *. |
| n | writes into the argument the number of characters read so far by this call; int *. No input is read. The converted item count is not incremented. |
| [...] | matches the longest non-empty string of input characters from the set between brackets; char *. A '\0' is added. []...] includes ] in the set. |
| [^...] | matches the longest non-empty string of input characters not from the set between brackets; char *. A '\0' is added. [^^]...] includes ] in the set. |
| % | literal %; no assignment is made. |
Many of the functions in the library set status indicators when error or end of file occur. These indicators may be set and tested explicitly. In addition, the integer expression errno (declared in ) may contain an error number that gives further information about the most recent error.
The header declares functions for testing characters. For each function, the argument list is an int, whose value must be EOF or representable as an unsigned char, and the return value is an int. The functions return non-zero (true) if the argument c satisfies the condition described, and zero if not.
| isalnum(c) | isalpha(c) or isdigit(c) is true |
| isalpha(c) | isupper(c) or islower(c) is true |
| iscntrl(c) | control character |
| isdigit(c) | decimal digit |
| isgraph(c) | printing character except space |
| islower(c) | lower-case letter |
| isprint(c) | printing character including space |
| ispunct(c) | printing character except space or letter or digit |
| isspace(c) | space, formfeed, newline, carriage return, tab, vertical tab |
| isupper(c) | upper-case letter |
| isxdigit(c) | hexadecimal digit |
In the seven-bit ASCII character set, the printing characters are 0x20 (' ') to 0x7E ('-'); the control characters are 0 NUL to 0x1F (US), and 0x7F (DEL).
In addition, there are two functions that convert the case of letters:
| int tolower(c) | convert c to lower case |
| int toupper(c) | convert c to upper case |
If c is an upper-case letter, tolower(c) returns the corresponding lower-case letter, toupper(c) returns the corresponding upper-case letter; otherwise it returns c.
There are two groups of string functions defined in the header . The first have names beginning with str; the second have names beginning with mem. Except for memmove, the behavior is undefined if copying takes place between overlapping objects. Comparison functions treat arguments as unsigned char arrays.
In the following table, variables s and t are of type char *; cs and ct are of type const char *; n is of type size_t; and c is an int converted to char.
| char *strcpy(s,ct) | copy string ct to string s, including '\0'; return s. |
| char *strncpy(s,ct,n) | copy at most n characters of string ct to s; return s. Pad with '\0''s if ct has fewer than n characters. |
| char *strcat(s,ct) | concatenate string ct to end of string s; return s. |
| char *strncat(s,ct,n) | concatenate at most n characters of string ct to string s, terminate s with '\0'; return s. |
| int strcmp(cs,ct) | compare string cs to string ct, return <0 if cs, 0 if cs==ct, or >0 if cs>ct. |
| int strncmp(cs,ct,n) | compare at most n characters of string cs to string ct; return <0 if cs, 0 if cs==ct, or >0 if cs>ct. |
| char *strchr(cs,c) | return pointer to first occurrence of c in cs or NULL if not present. |
| char *strrchr(cs,c) | return pointer to last occurrence of c in cs or NULL if not present. |
| size_t strspn(cs,ct) | return length of prefix of cs consisting of characters in ct. |
| size_t strcspn(cs,ct) | return length of prefix of cs consisting of characters not in ct. |
| char *strpbrk(cs,ct) | return pointer to first occurrence in string cs of any character string ct, or NULL if not present. |
| char *strstr(cs,ct) | return pointer to first occurrence of string ct in cs, or NULL if not present. |
| size_t strlen(cs) | return length of cs. |
| char *strerror(n) | return pointer to implementation-defined string corresponding to error n. |
| char *strtok(s,ct) | strtok searches s for tokens delimited by characters from ct; see below. |
A sequence of calls of strtok(s,ct) splits s into tokens, each delimited by a character from ct. The first call in a sequence has a non-NULL s, it finds the first token in s consisting of characters not in ct; it terminates that by overwriting the next character of s with '\0' and returns a pointer to the token. Each subsequent call, indicated by a NULL value of s, returns the next such token, searching from just past the end of the previous one. strtok returns NULL when no further token is found. The string ct may be different on each call.
The mem... functions are meant for manipulating objects as character arrays; the intent is an interface to efficient routines. In the following table, s and t are of type void *; cs and ct are of type const void *; n is of type size_t; and c is an int converted to an unsigned char.
| void *memcpy(s,ct,n) | copy n characters from ct to s, and return s. |
| void *memmove(s,ct,n) | same as memcpy except that it works even if the objects overlap. |
| int memcmp(cs,ct,n) | compare the first n characters of cs with ct; return as with strcmp. |
| void *memchr(cs,c,n) | return pointer to first occurrence of character c in cs, or NULL if not present among the first n characters. |
| void *memset(s,c,n) | place character c into first n characters of s, return s. |
The header declares mathematical functions and macros.
The macros EDOM and ERANGE (found in ) are non-zero integral constants that are used to signal domain and range errors for the functions; HUGE_VAL is a positive double value. A domain error occurs if an argument is outside the domain over which the function is defined. On a domain error, errno is set to EDOM; the return value is implementation-defined. A range erroroccurs if the result of the function cannot be represented as a double. If the result overflows, the function returns HUGE_VAL with the right sign, and errno is set to ERANGE. If the result underflows, the function returns zero; whether errno is set to ERANGE is implementation-defined.
In the following table, x and y are of type double, n is an int, and all functions return double. Angles for trigonometric functions are expressed in radians.
| sin(x) | sine of x |
| cos(x) | cosine of x |
| tan(x) | tangent of x |
| asin(x) | sin-1(x) in range [-pi/2,pi/2], x in [-1,1]. |
| acos(x) | cos-1(x) in range [0,pi], x in [-1,1]. |
| atan(x) | tan-1(x) in range [-pi/2,pi/2]. |
| atan2(y,x) | tan-1(y/x) in range [-pi,pi]. |
| sinh(x) | hyperbolic sine of x |
| cosh(x) | hyperbolic cosine of x |
| tanh(x) | hyperbolic tangent of x |
| exp(x) | exponential function ex |
| log(x) | natural logarithm ln(x), x>0. |
| log10(x) | base 10 logarithm log10(x), x>0. |
| pow(x,y) | xy. A domain error occurs if x=0and y<=0, or if x<0 and y is not an integer. |
| sqrt(x) | sqare root of x, x>=0. |
| ceil(x) | smallest integer not less than x, as a double. |
| floor(x) | largest integer not greater than x, as a double. |
| fabs(x) | absolute value |x| |
| ldexp(x,n) | x*2n |
| frexp(x, int *ip) | splits x into a normalized fraction in the interval [1/2,1) which is returned, and a power of 2, which is stored in *exp. If x is zero, both parts of the result are zero. |
| modf(x, double *ip) | splits x into integral and fractional parts, each with the same sign as x. It stores the integral part in *ip, and returns the fractional part. |
| fmod(x,y) | floating-point remainder of x/y, with the same sign as x. If y is zero, the result is implementation-defined. |
The header declares functions for number conversion, storage allocation, and similar tasks.
double atof(const char *s)
void *bsearch(const void *key, const void *base,
size_t n, size_t size,
int (*cmp)(const void *keyval, const void *datum))
void qsort(void *base, size_t n, size_t size,
int (*cmp)(const void *, const void *))
B.6 Diagnostics:The assert macro is used to add diagnostics to programs: void assert(int expression) If expression is zero when assert(expression) is executed, the assert macro will print on stderr a message, such as Assertion failed: expression, file filename, line nnn It then calls abort to terminate execution. The source filename and line number come from the preprocessor macros __FILE__ and __LINE__. If NDEBUG is defined at the time is included, the assert macro is ignored. B.7 Variable Argument Lists:The header provides facilities for stepping through a list of function arguments of unknown number and type. Suppose lastarg is the last named parameter of a function f with a variable number of arguments. Then declare within f a variable of type va_list that will point to each argument in turn: va_list ap; ap must be initialized once with the macro va_start before any unnamed argument is accessed: va_start(va_list ap,lastarg); Thereafter, each execution of the macro va_arg will produce a value that has the type and value of the next unnamed argument, and will also modify ap so the next use of va_arg returns the next argument: type va_arg(va_list ap,type); The macro void va_end(va_list ap); must be called once after the arguments have been processed but before f is exited. B.8 Non-local Jumps:The declarations in provide a way to avoid the normal function call and return sequence, typically to permit an immediate return from a deeply nested function call.
if (setjmp(env) == 0)
/* get here on direct call */
else
/* get here by calling longjmp */
B.9 Signals:The header provides facilities for handling exceptional conditions that arise during execution, such as an interrupt signal from an external source or an error in execution. void (*signal(int sig, void (*handler)(int)))(int) signal determines how subsequent signals will be handled. If handler is SIG_DFL, the implementation-defined default behavior is used, if it is SIG_IGN, the signal is ignored; otherwise, the function pointed to by handler will be called, with the argument of the type of signal. Valid signals include
signal returns the previous value of handler for the specific signal, or SIG_ERR if an error occurs. When a signal sig subsequently occurs, the signal is restored to its default behavior; then the signal-handler function is called, as if by (*handler)(sig). If the handler returns, execution will resume where it was when the signal occurred. The initial state of signals is implementation-defined. int raise(int sig) raise sends the signal sig to the program; it returns non-zero if unsuccessful. B.10 Date and Time Functions:The header declares types and functions for manipulating date and time. Some functions process local time, which may differ from calendar time, for example because of time zone. clock_t and time_t are arithmetic types representing times, and struct tm holds the components of a calendar time:
tm_isdst is positive if Daylight Saving Time is in effect, zero if not, and negative if the information is not available. The next four functions return pointers to static objects that may be overwritten by other calls.
Sun Jan 3 15:14:13 1988\n\0
asctime(localtime(tp))
B.11 Implementation-defined Limits: andThe header defines constants for the sizes of integral types. The values below are acceptable minimum magnitudes; larger values may be used.
The names in the table below, a subset of , are constants related to floating-point arithmetic. When a value is given, it represents the minimum magnitude for the corresponding quantity. Each implementation defines appropriate values.
|
上一页: 附录A 参考手册 返回上级目录: C语言教程 下一页: 变更概要
© 2008 woyouxian.net 版权所有 Contact Us