The <stdlib.h>
header file declares four types and
several functions of general use, and defines several macros. The
functions perform string conversion, random number generation,
searching and sorting, memory management, and similar tasks.
size_t
sizeof
operator.
wchar_t
div_t
div
function.
ldiv_t
ldiv
function.
NULL
EXIT_FAILURE
/EXIT_SUCCESS
exit
function to return unsuccessful or successful
termination status, respectively, to the host environment. These
macros are useful as return values from the main function as
well.
RAND_MAX
rand
function.
MB_CUR_MAX
LC_
TYPE
), and whose value is never greater than MB_
LEN_MAX
.
double atof(const char *nptr);
double
representation and returns the converted
value. Except for its behavior when an error occurs, this
function is equivalent to:
strtod(nptr, (char **)NULL)
int atoi(const char *nptr);
int
representation and returns the converted value.
Except for its behavior when an error occurs, this function is
equivalent to:
(int)strtol(nptr, (char **)NULL, 10)
long int atol(const char *nptr);
long
int
representation and returns the converted value.
Except for its behavior when an error occurs, this function is
equivalent to:
strtol(nptr, (char **)NULL, 10)
double strtod(const char *nptr, char
**endptr);
double
representation.
See your DEC C library routine documentation for a detailed description of this function.
long int strtol(const char *nptr, char
**endptr, int base);
long
int
representation.
See your DEC C library routine documentation for a detailed description of this function.
unsigned long int strtoul(const char *nptr, char
**endptr, int base);
unsigned long int
representation.
See your DEC C library routine documentation for a detailed description of this function.
Pseudo-Random Sequence Generation Functions
int rand(void);
RAND_MAX
.
void srand(unsigned int seed);
rand
. If srand
is then called with
the same seed value, the sequence of pseudo-random integers is
repeated. If rand
is called before any calls to
srand
are made, the sequence generated is the same
as when srand
is first called with a seed value of
1. The srand
function returns no value.
void *calloc(size_t nmemb, size_t
size);
calloc
function returns either a
null pointer if unable to allocate, or a pointer to the allocated
area.
void free(void *ptr);
calloc
,
malloc
, or realloc
. If ptr
is null, no action occurs. No value is returned.
void *malloc(size_t size);
void *realloc(void *ptr, size_t
size);
realloc
is identical to
malloc
. The contents of the area are unchanged up
to the lesser of the old and new sizes. This function returns
either a null pointer if unable to resize, or a pointer to the
possibly moved reallocated area.
Communication with the Environment
void abort(void);
SIGABRT
signal is being caught and the signal
handler does not return. The abort
function cannot
return to its caller.
int atexit(void (*func)(void));
atexit
function
returns 0 if the registration succeeds; otherwise, it returns
nonzero.
void exit(int status);
exit
, the behavior
is undefined. Upon execution, the following occurs:
atexit
are
called in the reverse order of their registration.
tmpfile
are
removed.
errno
value:
EXIT_
SUCCESS
, a successful termination status
is returned.
EXIT_
FAILURE
, an unsuccessful termination
status is returned.
char *getenv(const char *name);
See your DEC C library routine documentation for a detailed description of this function.
int *system(const char *string);
system
function returns nonzero if a command processor is available or 0
if one is not available. If the argument is not a null pointer,
the return value is the status returned by the command processor
or 0 if a command processor is not available.
See your DEC C library routine documentation for a detailed description of this function.
Searching and Sorting Utilities
void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *));
You must first sort the array in ascending order according to the
function pointed to by compar. The bsearch
function calls the specified comparison function pointed to by
compar with two arguments that point to the objects
being compared (the key object and an array element).
The comparison function returns:
The bsearch
function returns a pointer to the
matching element of the array, or a null pointer if no match
is found.
void qsort(void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *));
The contents of the array are sorted in ascending order according
to a comparison function pointed to by compar
,
which is called with two arguments that point to the objects
being compared. The comparison function returns:
If two compared elements are equal, their order in the sorted array is unspecified.
The qsort
function returns no value.
int abs(int j);
div_t div(int numer, int denom);
div
function
returns a structure of type div_t
containing the
quotient and remainder:
int quot; /* quotient */ int rem; /* remainder */
long int labs(long int j);
ldiv_t ldiv(long int numer, long int
denom);
div
function, except that the
arguments and the members of the returned structure (which has
type ldiv_t
) all have type long int
.
int mblen(const char *s, size_t n);
mblen
determines the number of bytes comprising the multibyte character
pointed to by s. The mblen
function is
equivalent to the following, except that the shift state of the
mbtowc
is not affected:
mbtowc((wchar_t *)0, s, n);
If s is a null pointer, the mblen
function
returns a nonzero value if multibyte character encodings have
state-dependent encodings, and 0 if they do not.
If s is not a null pointer, the mblen
function returns one of the following values:
int mbtowc(wchar_t *pwc, const char *s,
size_t n);
mbtowc
determines the number of bytes comprising the multibyte character
pointed to by s. It then determines the code for the
value of type wchar_t
that corresponds to that
multibyte character. (The value of the code corresponding to
the null character is 0.) If the multibyte character is valid
and pwc is not a null pointer, mbtowc
stores the code in the object pointed to by pwc. At
most, n bytes of the array pointed to by s are
examined.
If s is a null pointer, the mbtowc
function
returns a nonzero value if multibyte character encodings have
state-dependent encodings, and 0 if they do not.
If s is not a null pointer, the mbtowc
function returns one of the following values:
int wctomb(char *s, wchar_t wchar);
MB_CUR_MAX
characters are stored. If the value of wchar is 0, the
wctomb
function is left in the initial shift state.
If s is a null pointer, the wctomb
function
returns a nonzero value if multibyte character encodings have
state-dependent encodings, and 0 if they do not.
If s is not a null pointer, the wctomb
function returns one of the following values:
size_t mbstowcs(wchar_t *pwcs, const char
*s, size_t n);
mbtowc
, except that the shift state of
mbtowc
is not affected.
If an invalid multibyte character is encountered, the
mbstowcs
function returns (size_t) - 1
. Otherwise, it returns the number of array elements modified,
not including a terminating zero code, if any.
size_t wcstombs(char *s, const wchar_t
*pwcs, size_t n);
Each code is converted as if by a call to wctomb
, except that the shift state of wctomb
is not
affected.
If a code is encountered that does not correspond to a valid
multibyte character, the wcstombs
function returns
(size_t) - 1
. Otherwise, it returns the number of
bytes modified, not including a terminating null character, if
any.