Appendix B - Standard Library (p. 241)
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:
<assert.h> <float.h> <math.h> <stdarg.h> <stdlib.h>
<ctype.h> <limits.h> <setjmp.h> <stddef.h> <string.h>
<errno.h> <locale.h> <signal.h> <stdio.h> <time.h>
A header can be accessed by
#include <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.
B1 - Input and Output: <stdio.h>
The input and output functions, types, and macros defined in <stdio.h>
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 bas 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 opening it; 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.
B1.1 - File Operations
The following functions deal with operations on files. The type size_t
is the
unsigned integral type produced by the sizeof
operator.
FILE *fopen(const char *filename, const char *mode)
fopen
opens the named file, and returns a stream, orNULL
if the attempt fails. Legal values formode
include"r" open text file for reading
"w" create text file for writing; discard previous contents if any
"a" append; open or create text file for writing at end of file
"r+" open text file for update (i.e., reading and writing)
"w+" create text file for update; discard previous contents if any
"a+" append; open or create text file for update, writing at endUpdate mode permits reading and writing the same file;
fflush
or a file-positioning function must be called between a read and a write or vice versa. If the mode includesb
after the initial letter, as in"rb"
or"w+b"
, that indicates a binary file. Filenames are limited toFILENAME_MAX
characters. At mostFOPEN_MAX
files may be open at once.FILE *freopen(const char *filename, const char *mode, FILE *stream)
freopen
opens the file with the specified mode and associates the stream with it. It returnsstream
, orNULL
if an error occurs.freopen
is normally used to change the files associated withstdin
,stdout
, orstderr
.int fflush(FILE *stream)
On an output stream,
fflush
causes any buffered but unwritten data to be written; on an input stream, the effect is undefined. It returnsEOF
for a wrtte error, and zero otherwise.fflush(NULL)
flushes all output streams.int fclose(FILE *stream)
fclose
flushes any unwritten data forstream
, discards any unread buffered input, frees any automatically allocated buffer, then closes the stream. It returnsEOF
if any errors occurred, and zero otherwise.int remove(const char *filename)
remove
removes the named file, so that a subsequent attempt to open it will fail. It returns non-zero if the attempt fails.int rename(const char *oldname, const char *newname)
rename
changes the name of a file; it returns non-zero if the attempt fails.
FILE *tmpfile(void)
tmpfile
creates a temporary file of mode"wb+"
that will be automatically removed when closed or when the program terminates normally.tmpfile
returns a stream, orNULL
if it could not create the file.char *tmpnam(char s[L_tmpnam])
tmpnam(NULL)
creates a string that is not the name of an existing file, and returns a pointer to an internal static array.tmpnam(s)
stores the string ins
as well as returning it as the function value; s must have room for at leastL_tmpnam
characters.tmpnam
generates a different name each time it is called; at mostTMP_MAX
different names are guaranteed during execution of the program. Note thattmpnam
creates a name, not a file.int setvbuf(FILE *stream, char *buf, int mode, size_t size)
setvbuf
controls buffering for the stream; it must be called before reading, writing, or any other operation. Amode
of_IOFBF
causes full buffering,_IOLBF
line buffering of text files, and_IONBF
no buffering. Ifbuf
is notNULL
, it will be used as the buffer; otherwise a buffer will be allocated.size
determines the buffer size.setvbuf
returns non-zero for any error.void setbuf(FILE *stream, char *buf)
If
buf
isNULL
, buffering is turned off for the stream. Otherwise,setbuf
is equivalent to(void) setvbuf (stream, buf, _IOFBF, BUFSIZ)
.
B1.2 - Formatted Output
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:
Flags (in any order), which modify the specification:
-
, which specifies left adjustment of the converted argument in its field.+
, which specifies that the number will always be printed with a sign.space: if the first character is not a sign, a space will be prefixed.
0
: for numeric conversions, specifies padding to the field width with leading zeros.#
, which specifies an alternate output form. Foro
, the first digit will be zero. Forx
orX
,0x
or0X
will be prefixed to a non-zero result. Fore
,E
,f
,g
, andG
, the output will always have a decimal point; forg
andG
, trailing zeros will not be removed.A number specifying a minimum field width. The converted argument will be printed in a field at least this wide, and wider if necessary. If the converted argument has fewer characters than the field width it will be padded on the left (or right, if left adjustment has been requested) to make up the field width. The padding character is normally space, but is 0 if the zero padding flag is present.
A period, which separates the field width from the precision.
A number, the precision, that specifies the maximum number of characters to be printed from a string, or the number of digits to be printed after the decimal point for
e
,E
, orf
conversions, or the number of significant digits forg
orG
conversion, or the minimum number of digits to be printed for an integer (leading 0s will be added to make up the necessary width).A length modifier
h
,l
(letter ell), orL
. "h
" indicates that the corresponding argument is to be printed as ashort
orunsigned short
; "l
" indicates that the argument is along
orunsigned long
; "L
" indicates that the argument is along double
.
Width or precision or both may be specified as *
, in which case the value is computed
byconverting 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; converted to |
---|---|
d , i | int ; signed decimal notation. |
o | int ; unsigned octal notation (without a leading zero). |
x , X | 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 specified 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 % . |
int printf(const char *format, ... )
printf( ... )
is equivalent tofprintf(stdout, ... )
.int sprintf(char *s, const char *format, ... )
sprintf
is the same asprintf
except that the output is written into the strings
, terminated with'\0'
.s
must be big enough to hold the result. The return count does not include the'\0'
.vprintf(const char *format, va_list arg)
vfprintf(FILE *stream, const char *format, va_list arg)
vsprintf(char *s, const char *format, va_list arg)
The functions
vprintf
,vfprintf
, andvsprintf
are equivalent to the correspondingprintf
functions, except that the variable argument list is replaced by arg
, which has been initialized by theva_start
macro and perhapsva_arg
calls. See the discussion of<stdarg.h>
in Section B7.
B1.3 - Formatted Input
The scanf
functions deal 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:
- Blanks or tabs, which are ignored.
- Ordinary characters (not
%
), which are expected to match the next non-white space character of the input stream. - Conversion specifications, consisting of a
%
, an optional assignment suppression character*
, an optional number specifying a maximum field width, an optionalh
,l
, orL
indicating the width of the target, and a conversion character.
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. |
int scanf(const char *format, ... )
scanf(...)
is identical tofscanf(stdin, ... )
.int sscanf(char *s, const char *format, ... )
sscanf(s, ... )
is equivalent toscanf( ... )
except that the input characters are taken from the strings
.
B1.4 - Character Input and Output Functions
int fgetc(FILE *stream)
fgetc
returns the next character ofstream
as anunsigned char
(converted to anint
), orEOF
if end of file or error occurs.char *fgets(char *s, int n, FILE *stream)
fgets
reads at most the nextn - 1
characters into the arrays
, stopping if a newline is encountered; the newline is included in the array, which is terminated by'\0'
.fgets
returnss
, orNULL
if end of file or error occurs.int fputc(int c, FILE *stream)
fputc
writes the characterc
(converted to anunsigned char
) onstream
. It returns the character written, orEOF
for error.int fputs(const char *s, FILE *stream)
fputs
writes the strings
(which need not contain'\n'
) onstream
; it returns non-negative, orEOF
for an error.int getc(FILE *stream)
getc
is equivalent tofgetc
except that if it is a macro, it may evaluatestream
more than once.int getchar(void}
getchar
is equivalent togetc(stdin)
.char *gets(char *S)
gets
reads the next input line into the arrays
; it replaces the terminating newline with'\0'
. It returnss
, orNULL
if end of file or error occurs.int putc(int c, FILE *stream)
putc
is equivalent tofputc
except that if it is a macro, it may evaluatestream
more than once.int putchar(int c)
putchar(c)
is equivalent toputc(c, stdout)
.int puts(const char *S}
puts
writes the strings
and a newline tostdout
. It returnsEOF
if an error occurs, non-negative otherwise.int ungetc(int c, FILE *stream)
ungetc
pushesc
(converted to anunsigned char
back ontostream
, where it will be returned on the next read. Only one character of pushback per stream is guaranteed.EOF
may not be pushed back.ungetc
returns the character pushed back, orEOF
for error.
B1.5 - Direct Input and Output Functions
size_t fread(void *ptr, size_t size, size_t nobj, FILE *stream)
fread
reads fromstream
into the arrayptr
at mostnobj
objects of sizesize
.fread
returns the number of objects read; this may be less than the number requested.feof
andferror
must be used to determine status.size_t fwrite(const void *ptr, size_t size, size_t nobj, FILE *stream)
fwrite
writes, from the arrayptr
,nobj
objects of sizesize
onstream
. It returns the number of objects written, which is less thannobj
on error.
B1.6 - File Positioning Functions
int fseek(FILE *stream, long offset, int origin)
fseek
sets the file position forstream
; a subsequent read or write will access data beginning at the new position. For a binary file, the position is set tooffset
characters fromorigin
, which may beSEEK_SET
(beginning),SEEK_CUR
(current position), orSEEK_END
(end of file). For a text stream,offset
must be zero, or a value returned byftell
(in which case origin must beSEEK_SET
).fseek
returns non-zero on error.long ftell(FILE *stream)
ftell
returns the current file position forstream
, or-1L
on error.void rewind(FILE *stream)
rewind(fp)
is equivalent tofseek(fp, 0L, SEEK_SET); clearerr(fp)
.int fgetpos(FILE *stream, fpos_t *Ptr)
fgetpos
records the current position instream
in*ptr
, for subsequent use byfsetpos
. The typefpos_t
is suitable for recording such values.fgetpos
returns non-zero on error.int fsetpos(FILE *stream, const fpos_t *Ptr)
fsetpos
positionsstream
at the position recorded byfgetpos
in*ptr
.fsetpos
returns non-zero on error.
B1.7 - Error Functions
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 <errno.h>
) may contain an error number that gives further
information about the most recent error.
void clearerr(FILE *stream)
clearerr
clears the end of file and error indicators forstream
.int feof(FILE *stream)
feof
returns non-zero if the end of file indicator forstream
is set.int ferror(FILE *stream)
ferror
returns non-zero if the error indicator forstream
is set.void perror(const char *S)
perror(s)
printss
and an implementation-defined error message corresponding to the integer inerrno
, as if byfprintf(stderr, "%s: %s\n", s, "error message")
See
strerror
in Section B3.
B2 - Character Class Tests: <ctype.h>
The header <ctype.h>
declares functions for testing characters. For each function,
the argument 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.
Function | Description |
---|---|
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(int c) convert c to lower case
int toupper(int c) convert c to upper case
If c
is an upper-case letter, tolower(c)
returns the corresponding lower-case letter;
otherwise it returns c
. If c
is a lower-case letter, toupper(c)
returns the corresponding
upper-case letter; otherwise it returns c
.
B3 - String Functions: <string.h>
There are two groups of string functions defined in the header <string.h>
. 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
.
Command | Description |
---|---|
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 t 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 < ct , 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 < ct , 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 of string ct , or NULL if none are 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
.
Command | Description |
---|---|
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 . |
B4 - Mathematical Functions: <math.h>
The header <math.h>
declares mathematical functions and macros.
The macros EDOM
and ERANGE
(found in <errno.h>
) 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-dependent. A range error occurs 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.
Function | Description |
---|---|
sin(x) | sine of |
cos(x) | cosine of |
tan(x) | tangent of |
asin(x) | in range , |
acos(x) | in range , |
atan(x) | in range |
atan2(y,x) | in range |
sinh(x) | hyperbolic sine of |
cosh(x) | hyperbolic cosine of |
tanh(x) | hyperbolic tangent of |
exp(x) | exponential function |
log(x) | natural logarithm , |
log10(x) | base 10 logarithm , |
pow(x,y) | . A domain error occurs if and , or if and is not an integer |
sqrt(x) | , |
ceil(x) | smallest integer not less than , as a double |
floor(x) | largest integer not greater than , as a double |
fabs(x) | absolute value $ |
ldexp(x,n) | |
frexp(x, int *exp) | splits into a normalized fraction in the interval , which is returned, and a power of 2, which is stored in *exp . If is zero, both parts of the result are zero. |
modf(x, double *ip) | splits into integral and fractional parts, each with the same sign as . It stores the integral part in *ip , and returns the fractional part. |
fmod(x, y) | floating-point remainder of , with the same sign as . If is zero, the result is implementation-defined. |
B5 - Utility Functions: <stdlib.h>
The header <stdlib.h>
declares functions for number conversion, storage allocation,
and similar tasks.
double atof(const char *s)
atof
convertss
todouble
; it is equivalent tostrtod(s, (char**)NULL)
.int atoi(con8t char *S)
converts
s
toint
; it is equivalent to(int)strtol(s, (char**)NULL, 10)
.long atol(con8t char *8)
converts
s
tolong
; it is equivalent tostrtol(s, (char**)NULL, 10)
.double 8trtod(const char *S, char **endp)
strtod
converts the prefix ofs
todouble
, ignoring leading white space; it stores a pointer to any unconverted suffix in*endp
unlessendp
isNULL
. If the answer would overflow,HUGE_VAL
is returned with the proper sign; if the answer would underflow, zero is returned. In either case errno is set toERANGE
.long strtol(const char *s, char **endp, int base)
strtol
converts the prefix ofs
tolong
, ignoring leading white space; it stores a pointer to any unconverted suffix in*endp
unlessendp
isNULL
. Ifbase
is between 2 and 36, conversion is done assuming that the input is written in that base. Ifbase
is zero, the base is 8, 10, or 16; leading 0 implies octal and leading0x
or0X
hexadecimal. Letters in either case represent digits from 10 tobase-1
; a leading0x
or0X
is permitted in base 16. If the answer would overflow,LONG_MAX
orLONG_MIN
is returned, depending on the sign of the result, anderrno
is set toERANGE
.unsigned long strtoul(const char *s, char **endp, int base)
strtoul
is the same asstrtol
except that the result isunsigned long
and the error value isULONG_MAX
.int rand(void)
rand
returns a pseudo-random integer in the range 0 toRAND_MAX
, which is at least 32767.void srand(unsigned int seed)
srand
usesseed
as the seed for a new sequence of pseudo-random numbers. The initial seed is 1.void *calloc(size_t nobj, size_t size)
calloc
returns a pointer to space for an array ofnobj
objects, each of sizesize
, orNULL
if the request cannot be satisfied. The space is initialized to zero bytes.void *malloc(size_t size)
malloc
returns a pointer to space for an object of sizesize
, orNULL
if the request cannot be satisfied. The space is uninitialized.void *realloc(void *p, size_t size)
realloc
changes the size of the object pointed to byp
tosize
. The contents will be unchanged up to the minimum of the old and new sizes. If the new size is larger, the new space is uninitialized.realloc
returns a pointer to the new space, orNULL
if the request cannot be satisfied, in which case*p
is unchanged.void free(void *p)
free
deallocates the space pointed to byp
; it does nothing ifp
isNULL
.p
must be a pointer to space previously allocated bycalloc
,malloc
, orrealloc
.void abort(void)
abort
causes the program to terminate abnormally, as if byraise(SIGABRT)
.void exit(int status)
exit
causes normal program termination.atexit
functions are called in reverse order of registration, open files are flushed, open streams are closed, and control is returned to the environment. Howstatus
is returned to the environment is implementation-dependent, but zero is taken as successful termination. The valuesEXIT_SUCCESS
andEXIT_FAILURE
may also be used.int atexit(void (*fcn)(void))
atexit
registers the functionfcn
to be called when the program terminates normally; it returns non-zero if the registration cannot be made.int system(const char *s)
system
passes the strings
to the environment for execution. Ifs
isNULL
, system returns non-zero if there is a command processor. Ifs
is notNULL
, the return value is implementation-dependent.char *getenv(const char *name)
getenv
returns the environment string associated withname
, orNULL
if no string exists. Details are implementation-dependent.void *bsearch(const void *key, const void *base, size_t n, size_t size, int (*cmp)(const void *keyval, const void *datum))
bsearch
searchesbase[0] ... base[n-1]
for an item that matches*key
. The functioncmp
must return negative if its first argument (the search key) is less than its second (a table entry), zero if equal, and positive if greater. Items in the arraybase
must be in ascending order.bsearch
returns a pointer to a matching item, orNULL
if none exists.void qsort(void *base, size_t n, size_t size, int (*cmp)(const void *, const void *))
qsort
sorts into ascending order an arraybase[0] ... base[n-1]
of objects of sizesize
. The comparison functioncmp
is as inbsearch
.int abs(int n)
abs
returns the absolute value of itsint
argument.long labs(long n)
labs
returns the absolute value of itslong
argument.div_t div(int num, int denom)
div
computes the quotient and remainder ofnum / denom
. The results are stored in theint
membersquot
andrem
of a structure of typediv_t
.ldiv_t ldiv(long num, long denom)
div
computes the quotient and remainder ofnum / denom
. The results are stored in thelong
membersquot
andrem
of a structure of typeldiv_t
.
B6 - Diagnostics: <assert.h>
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 <assert.h>
is included, the assert
macro is
ignored.
B7 - Variable Argument Lists: <stdarg.h>
The header <stdarg.h>
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 ap
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.
B8 - Non-local Jumps: <setjmp.h>
The declarations in <setjmp.h>
provide a way to avoid the normal function call
and return sequence, typically to permit an immediate return from a deeply nested func- ·
tion call.
int setjmp(jmp_buf env)
The macro
setjmp
saves state information inenv
for use bylongjmp
. The return is zero from a direct call ofsetjmp
, and non-zero from a subsequent call oflongjmp
. A call tosetjmp
can only occur in certain contexts, basically the test ofif
,switch
, and loops, and only in simple relational expressions.if (setjmp(env) == 0)
/* get here on direct call */
else
/* get here by calling longjmp */void longjmp(jmp_buf env, int val)
longjmp
restores the state saved by the most recent call tosetjmp
, using information saved inenv
, and execution resumes as if thesetjmp
function had just executed and returned the non-zero valueval
. The function containing thesetjmp
must not have terminated. Accessible objects have the values they had whenlongjmp
was called, except that non-volatile
automatic variables in the function callingsetjmp
become undefined if they were changed after thesetjmp
call.
B9 - Signals: <signal.h>
The header <signal.h>
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
SIGABRT abnormal termination, e.g., from abort
SIGFPE arithmetic error, e.g., zero divide or overflow
SIGILL illegal function image, e.g., illegal instruction
SIGINT interactive attention, e.g., interrupt
SIGSEGV illegal storage access, e.g., access outside memory limits
SIGTERM termination request sent to this program
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.
B10 - Date and Time Functions: <time.h>
The header <time.h>
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:
Component | Description |
---|---|
int tm_sec; | seconds after the minute (0, 61) |
int tm_min; | minutes after the hour (0, 59) |
int tm_hour; | hours since midnight (0, 23) |
int tm_mday; | day of the month (1, 31) |
int tm_mon; | months since January (0, 11) |
int tm_year; | years since 1900 |
int tm_wday; | days since Sunday (0, 6) |
int tm_yday; | days since January 1 (0, 365) |
int tm_isdst; | Daylight Saving Time flag |
tm_isdst
is positive if Daylight Saving Time is in effect, zero if not, and negative if
the information is not available.
clock_t clock(void)
clock
returns the processor time used by the program since the beginning of execution, or-1
if unavailable.clock() / CLOCKS_PER_SEC
is a time in seconds.time_t time(time_t *tp)
time
returns the current calendar time or-1
if the time is not available. Iftp
is notNULL
, the return value is also assigned to*tp
.double difftime(time_t time2, time_t time1)
difftime
returnstime2-time1
expressed in seconds.time_t mktime(struct tm *tp)
mktime
converts the local time in the structure*tp
into calendar time in the same representation used bytime
. The components will have values in the ranges shown.mktime
returns the calendar time or-1
if it cannot be represented.
The next four functions return pointers to static objects that may be overwritten by other calls.
char *asctime(const struct tm *tp)
asctime
converts the time in the structure*tp
into a string of the formSun Jan 3 15:14:13 1988'n'O
char *ctime(const time_t *tp)
ctime
converts the calendar time*tp
to local time; it is equivalent toasctime(localtime(tp))
struct tm *gmtime(const time_t *tp)
gmtime
converts the calendar time*tp
into Coordinated Universal Time (UTC). It returnsNULL
if UTC is not available. The namegmtime
has historical significance.struct tm *localtime(const time_t *tp)
localtime
converts the calendar time*tp
into local time.size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp)
strftime
formats date and time information from*tp
intos
according tofmt
, which is analogous to aprintf
format. Ordinary characters (including the terminating'0'
) are copied intos
. Each%c
is replaced as described below, using values appropriate for the local environment. No more thansmax
characters are placed intos
.strftime
returns the number of characters, excluding the'0'
, or zero if more thansmax
characters were produced.
%a abbreviated weekday name.
%A full weekday name.
%b abbreviated month name.
%B full month name.
%c local date and time representation.
%d day of the month (01-31).
%H hour (24-hour clock) (00-23).
%I hour (12-hour clock) (01-12).
%j day of the year (001-366).
%m month (01-12).
%M minute (00-59).
%p local equivalent of AM or PM.
%S second (00-61).
%U week number of the year (Sunday as 1st day of week) (00-53).
%w weekday (0-6, Sunday is 0).
%W week number of the year (Monday as 1st day of week) (00-53).
%x local date representation.
%X local time representation.
%y year without century (00-99).
%Y year with century.
%Z time zone name, if any.
%% %
B11 - Implementation-defined Limits: <limits.h> and <float.h>
The header <limits.h>
defines constants for the sizes of integral types. The
values below are acceptable minimum magnitudes; larger values may be used.
CHAR_BIT 8 bits in a char
CHAR_MAX UCHAR_MAX or
SCHAR_MAX maximum value of char
CHAR_MIN 0 or SCHAR_MIN minimum value of char
INT_MAX +32767 maximum value of int
INT_MIN -32767 minimum value of int
LONG_MAX +2147483647 maximum value of long
LONG_MIN -2147483647 minimum value of long
SCHAR_MAX +127 maximum value of signed char
SCHAR_MIN -127 minimum value of signed char
SHRT_MAX +32767 maximum value of short
SHRT_MIN -32767 minimum value of short
UCHAR_MAX 255 maximum value of unsigned char
UINT_MAX 65535 maximum value of unsigned int
ULONG_MAX 4294967295 maximum value of unsigned long
USHRT_MAX 65535 maximum value of unsigned short
The names in the table below, a subset of <float.h>
, 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.
FLT_RADIX 2 radix of exponent representation, e.g., 2, 16
FLT_ROUNDS floating-point rounding mode for addition
FLT_DIG 6 decimal digits of precision
FLT_EPSILON 1E-5 smallest number x such that 1.0 + x != 1.0
FLT_MANT_DIG number of base FLT_RADIX digits in mantissa
FLT_MAX 1E+37 maximum floating-point number
FLT_MAX_EXP maximum n such that FLT_RADIX^n - 1 is representable
FLT_MIN 1E-37 minimum normalized floating-point number
FLT_MIN_EXP minimum n such that 10^n is a normalized number
DBL_DIG 10 decimal digits of precision
DBL_EPSILON 1E-9 smallest number x such that 1.0 + != 1.0
DBL_MANT_DIG number of base FLT_RADIX digits in mantissa
DBL_MAX 1E+37 maximum double floating-point number
DBL_MAX_EXP maximum n such that FLT_RADIX^n - 1 is representable
DBL_MIN 1E-37 minimum normalized double floating-point number
DBL_MIN_EXP minimum n such that 10^n is a normalized number