Site hosted by Angelfire.com: Build your free website today!

Table of Contents Previous Next


1.13 The Standard Headers: <stdlib.h>

The header <stdlib.h> declares functions for number conversion, storage allocation, and similar tasks.


double atof(const char *s);
atof converts s to double; it is equivalent to strtod(s, (char **)NULL).

Returns
s converted to double.


int atoi(const char *s);
Converts s to int; it is equivalent to (int)strtol(s, (char **)NULL, 10).

Returns
s converted to int.


long atol(const char *s);
Converts s to long; it is equivalent to strtol(s, (char **)NULL, 10).

Returns
s converted to long.


double strtod(const char *s, char **endp);
Converts the prefix of s to double, ignoring leading white space; it stores a pointer to any unconverted suffix in *endp unless endp is NULL.

Returns
If the answer would overflow, HUGH_VAL is returned with the proper sign; if the answer would underflow, zero is returned.  In either case, errno is set to ERANGE.


long strtol(const char *s, char **endp, int base);
Converts the prefix of s to long, ignoring leading white space; it stores a pointer to any unconverted suffix in *endp unless endp is NULL.  If base is between 2 and 32 , conversion is done assuming that the input is written in that base.  If base is zero, the base is 8, 10, or 16; leading 0 implies octal and leading 0x or 0X hexadecimal.  Letters in either case represent digits from 10 to base-1; a leading 0x or 0X is permitted in base 16. 

Returns
The converted prefix of s as explained above.  If the answer would overflow, LONG_MAX or LONG_MIN is returned, depending on the sign of the result, and errno is set to ERANGE.


unsigned long strtoul(const char *s, char **endp, int base);
strtoul is the same as strtol except that the result is unsigned long and the error is ULONG_MAX

Returns
The converted prefix of s as explained above.  If the answer would overflow, ULONG_MAX is returned and errno is set to ERANGE.


int rand(void);
rand generates a pseudo-random integer in the range 0 to RAND_MAX, which is at least 32767.

Returns
A pseudo random integer in the range 0 to RAND_MAX.


void srand(unsigned int seed);
srand uses seed 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 allocates space for an array of objects.

Returns
calloc returns a pointer to space for an array of nobj objects, each of size size, or NULL if the request cannot be satisfied.  The space is initialised to zero bytes.


void *malloc(size_t size);
malloc allocates space for an object.

Returns
malloc returns a pointer to space for an object of size size, or NULL if the request cannot be satisfied.  The space is uninitialised.


void *realloc(void *p, size_t size);
realloc changes the size of the object pointed to by p to size.  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 uninitialised.

Returns
realloc returns a pointer to the new space or NULL if the request cannot be satisfied, in which case *p is unchanged.


void free(void *p);
free deallocates the space pointed to by p; it does nothing if p is NULLp must be a pointer to space previously allocated by calloc, malloc, or realloc.


void abort(void);
abort causes the program to terminate abnormally, as if by raise(SIGABORT).


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.  How status is returned to the environment is implementation-dependent, but zero is taken as successful termination.  The values EXIT_SUCCESS and EXIT_FAILURE may also be used.


int atexit(void (*fcn)(void);
atexit registers the function fcn to be called when the program terminates normally.

Returns
atexit returns non-zero if the registration cannot be made.


int system(const char *s)
system passes the string s to the environment for execution. 

Returns
If s is NULL, system returns non-zero if there is a command processor.  If s is not NULL, the return value is implementation-dependent.


char *getenv(const char *name)
getenv returns the environment string associated with name, or NULL 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 searches base[0]...base[n-1] for an item that matches *key.  The function cmp 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 array base must be in ascending order.

Returns
bsearch returns a pointer to the matching item, or NULL 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 array base[0]...base[n-1] of objects of size size.  The comparison function cmp 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.


int abs(int n)
abs returns the absolute value of its int argument.


long labs(long n)
labs returns the absolute value of its long argument.


div_t div(long num, int denom)
div computes the quotient and remainder of num/denom.  The results are stored in the int members quot and rem of a structure of type div_t.


ldiv_t ldiv(long num, long denom)
ldiv computes the quotient and remainder of num/denom.  The results are stored in the long members quot and rem of a structure of type ldiv_t.

int mblen(const char *s, size_t n)
If s is not a null pointer, mblen determines the number of bytes contained in the multibyte character pointed to by s.  Except that the shift state of the mbtowc function is not affected, it is equivalent to
   mbtowc((wchar_t *)0, s, n);

size_t mbstowcs(wchar_t *pwcs, const char *s, size_t n)
mbstowcs converts a sequence of multibyte characters that begins in the initial shift state from the array pointed to by s into a sequence of corresponding codes and stores not more than n codes into the array pointed to by pwcs.  No multibyte characters that follow a null character (which is converted into a code with value zero) will be examined or converted.  Each multibyte character is converted as if by a call to the mbtowc function, except that the shift state of the mbtowc function is not affected.

No more than n elements will be modified in the array pointed to by pwcs.  If copying takes place between objects that overlap, the behaviour is undefined.

Returns
If an invalid multibyte character is encountered, the mbstowcs function returns (size_t)-1.  Otherwise, the mbstowcs function returns the number of array elements modified, not including a terminating zero code, if any.


int mbtowc(wchar_t *pwc, const char *s, size_t n)
If s is not a null pointer, mbtowc determines the number of bytes that are contained in 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.  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 will be examined.
Returns
If s is a null pointer, mbtowc returns a nonzero or zero value, if multibyte character encodings, respectively, do or do not have state-dependent encodings.  If s is not a null pointer, mbtowc either returns 0 (if s points to the null character), or returns the number of bytes that are contained in the converted multibyte character (if the next n or fewer bytes form a multibyte character), or returns -1 (if they do not form a valid multibyte character).

In no case will the value returned be greater than n or the value of the MB_CUR_MAX macro.


size_t wcstombs(char *s, const wchar_t *pwcs, size_t n)
wcstombs converts a sequence of codes that correspond to multibyte characters from array pwcs into a sequence of multibye characters that begins in the initial shift state and stores these multibyte characters in s, stopping if a multibyte characters would exceed the limit of n total bytes or if a null character is stored.  Each code is converted as if by a call to the wctomb function, except that the shift state of the wctomb function is not affected.
Returns
If a code is encountered that does not correspond to a valid multibyte character, the wcstombs function returns (size_t)-1.  Otherwise, the wcstombs function returns the number of bytes modified, not including a terminating null character, if any.

int wctomb(char *s, wchar_t *wchar)
wctomb determines the number of bytes needed to represent the multibyte character corresponding to the code whose value is wchar (including any change in shift state).  It stores the multibyte character representation in s (if s in not a null pointer).  At most MB_CUR_MAX characters are stored.  If the value of wchar is zero, wctomb is left in the initial shift state.
Returns
If s is a null pointer, wctomb returns a nonzero or zero value, if multibyte character encodings, respectively, do or do not have state-dependent encodings.  If s is not a null pointer, wctomb returns -1 if the value of wchar does not correspond to a valid multibyte character, or returns the number of bytes that are contained in the multibyte character corresponding to the value of wchar.

In no case will the value returned be greater than the value of the MB_CUR_MAX macro.


Table of Contents Previous Next

Last modified: Thu Dec 7 11:17:45 2000