Previous Section 2.12 stdio.h |
| Table of Contents | Index | |
Next Section
2.14 string.h |
The stdlib header defines several general operation functions and macros.
Macros:
NULL
EXIT_FAILURE
EXIT_SUCCESS
RAND_MAX
MB_CUR_MAX
Variables:
typedef size_t
typedef wchar_t
struct div_t
struct ldiv_t
Functions:
abort();
abs();
atexit();
atof();
atoi();
atol();
bsearch();
calloc();
div();
exit();
free();
getenv();
labs();
ldiv();
malloc();
mblen();
mbstowcs();
mbtowc();
qsort();
rand();
realloc();
srand();
strtod();
strtol();
strtoul();
system();
wcstombs();
wctomb();
size_t
is the unsigned integer result of the sizeof
keyword.
wchar_t
is an integer type of the size of a wide character constant.
div_t
is the structure returned by the div
function.
ldiv_t
is the structure returned by the ldiv
function.
NULL
is the value of a null pointer constant.
EXIT_FAILURE
and EXIT_SUCCESS
are values for the exit function to return
termination status.
RAND_MAX
is the maximum value returned by the rand function.
MB_CUR_MAX
is the maximum number of bytes in a multibyte character set which
cannot be larger than MB_LEN_MAX
.
Declaration:
The string pointed to by the argument str is converted to a floating-point number (typedouble atof(const char *
str);
double
). Any initial whitespace characters are skipped (space, tab, carriage return, new line,
vertical tab, or formfeed). The number may consist of an optional sign, a string of digits with an
optional decimal character, and an optional e
or E
followed by a optionally signed exponent.
Conversion stops when the first unrecognized character is reached.
On success the converted number is returned. If no conversion can be made, zero is
returned. If the value is out of range of the type double, then HUGE_VAL
is returned with the
appropriate sign and ERANGE
is stored in the variable errno
. If the value is too small to be
returned in the type double
, then zero is returned and ERANGE
is stored in the variable errno
.
Declaration:
The string pointed to by the argument str is converted to an integer (typeint atoi(const char *
str);
int
). Any initial
whitespace characters are skipped (space, tab, carriage return, new line, vertical tab, or
formfeed). The number may consist of an optional sign and a string of digits. Conversion stops
when the first unrecognized character is reached.
On success the converted number is returned. If the number cannot be converted, then 0
is returned.
Declaration:
The string pointed to by the argument str is converted to a long integer (typelong int atol(const char *
str);
long int
).
Any initial whitespace characters are skipped (space, tab, carriage return, new line, vertical tab,
or formfeed). The number may consist of an optional sign and a string of digits. Conversion
stops when the first unrecognized character is reached.
On success the converted number is returned. If the number cannot be converted, then 0
is returned.
Declaration:
The string pointed to by the argument str is converted to a floating-point number (typedouble strtod(const char *
str, char **
endptr);
double
). Any initial whitespace characters are skipped (space, tab, carriage return, new line,
vertical tab, or formfeed). The number may consist of an optional sign, a string of digits with an
optional decimal character, and an optional e
or E
followed by a optionally signed exponent.
Conversion stops when the first unrecognized character is reached.
The argument endptr is a pointer to a pointer. The address of the character that stopped the scan is stored in the pointer that endptr points to.
On success the converted number is returned. If no conversion can be made, zero is
returned. If the value is out of range of the type double, then HUGE_VAL
is returned with the
appropriate sign and ERANGE
is stored in the variable errno
. If the value is too small to be
returned in the type double
, then zero is returned and ERANGE
is stored in the variable errno
.
Declaration:
The string pointed to by the argument str is converted to a long integer (typelong int strtol(const char *
str, char **
endptr, int
base);
long int
).
Any initial whitespace characters are skipped (space, tab, carriage return, new line, vertical tab,
or formfeed). The number may consist of an optional sign and a string of digits. Conversion
stops when the first unrecognized character is reached.
If the base (radix) argument is zero, then the conversion is dependent on the first two characters. If the first character is a digit from 1 to 9, then it is base 10. If the first digit is a zero and the second digit is a digit from 1 to 7, then it is base 8 (octal). If the first digit is a zero and the second character is an x or X, then it is base 16 (hexadecimal).
If the base argument is from 2 to 36, then that base (radix) is used and any characters that fall outside of that base definition are considered unconvertible. For base 11 to 36, the characters A to Z (or a to z) are used. If the base is 16, then the characters 0x or 0X may precede the number.
The argument endptr is a pointer to a pointer. The address of the character that stopped the scan is stored in the pointer that endptr points to.
On success the converted number is returned. If no conversion can be made, zero is
returned. If the value is out of the range of the type long int
, then LONG_MAX
or LONG_MIN
is returned with the sign of the correct value and ERANGE
is stored in the variable errno
.
Declaration:
The string pointed to by the argument str is converted to an unsigned long integer (typeunsigned long int strtoul(const char *
str, char **
endptr, int
base);
unsigned long int
). Any initial whitespace characters are skipped (space, tab, carriage return,
new line, vertical tab, or formfeed). The number may consist of an optional sign and a string of
digits. Conversion stops when the first unrecognized character is reached.
If the base (radix) argument is zero, then the conversion is dependent on the first two characters. If the first character is a digit from 1 to 9, then it is base 10. If the first digit is a zero and the second digit is a digit from 1 to 7, then it is base 8 (octal). If the first digit is a zero and the second character is an x or X, then it is base 16 (hexadecimal).
If the base argument is from 2 to 36, then that base (radix) is used and any characters that fall outside of that base definition are considered unconvertible. For base 11 to 36, the characters A to Z (or a to z) are used. If the base is 16, then the characters 0x or 0X may precede the number.
The argument endptr is a pointer to a pointer. The address of the character that stopped the scan is stored in the pointer that endptr points to.
On success the converted number is returned. If no conversion can be made, zero is
returned. If the value is out of the range of the type unsigned long int
, then ULONG_MAX
is
returned and ERANGE
is stored in the variable errno
.
Declaration:
Allocates the requested memory and returns a pointer to it. The requested size is nitems each size bytes long (total memory requested is nitems*size). The space is initialized to all zero bits.void *calloc(size_t
nitems, size_t
size);
On success a pointer to the requested space is returned. On failure a null pointer is returned.
Declaration:
Deallocates the memory previously allocated by a call tovoid free(void *
ptr);
calloc
, malloc
, or realloc
. The
argument ptr points to the space that was previously allocated. If ptr points to a memory block
that was not allocated with calloc
, malloc
, or realloc
, or is a space that has been deallocated,
then the result is undefined.
Declaration:
Allocates the requested memory and returns a pointer to it. The requested size is size bytes. The value of the space is indeterminate.void *malloc(size_t
size);
On success a pointer to the requested space is returned. On failure a null pointer is returned.
Declaration:
Attempts to resize the memory block pointed to by ptr that was previously allocated with a call tovoid *realloc(void *
ptr, size_t
size);
malloc
or calloc
. The contents pointed to by ptr are unchanged. If the value of size is
greater than the previous size of the block, then the additional bytes have an undeterminate
value. If the value of size is less than the previous size of the block, then the difference of bytes
at the end of the block are freed. If ptr is null, then it behaves like malloc
. If ptr points to a
memory block that was not allocated with calloc
or malloc
, or is a space that has been
deallocated, then the result is undefined. If the new space cannot be allocated, then the contents
pointed to by ptr are unchanged. If size is zero, then the memory block is completely freed.
On success a pointer to the memory block is returned (which may be in a different location as before). On failure or if size is zero, a null pointer is returned.
Declaration:
void abort(void);
Causes an abnormal program termination. Raises the SIGABRT
signal and an
unsuccessful termination status is returned to the environment. Whether or not open streams are
closed is implementation-defined.
Declaration:
Causes the specified function to be called when the program terminates normally. At least 32 functions can be registered to be called when the program terminates. They are called in a last-in, first-out basis (the last function registered is called first).int atexit(void (*
func)(void));
On success zero is returned. On failure a nonzero value is returned.
Declaration:
Causes the program to terminate normally. First the functions registered by atexit are called, then all open streams are flushed and closed, and all temporary files opened with tmpfile are removed. The value of status is returned to the environment. If status isvoid exit(int
status);
EXIT_SUCCESS
,
then this signifies a successful termination. If status is EXIT_FAILURE
, then this signifies an
unsuccessful termination. All other values are implementation-defined.
Declaration:
Searches for the environment string pointed to by name and returns the associated value to the string. This returned value should not be written to.char *getenv(const char *
name);
If the string is found, then a pointer to the string's associated value is returned. If the string is not found, then a null pointer is returned.
Declaration:
The command specified by string is passed to the host environment to be executed by the command processor. A null pointer can be used to inquire whether or not the command processor exists.int system(const char *
string);
If string is a null pointer and the command processor exists, then zero is returned. All other return values are implementation-defined.
Declaration:
Performs a binary search. The beginning of the array is pointed to by base. It searches for an element equal to that pointed to by key. The array is nitems long with each element in the array size bytes long.void *bsearch(const void *
key, const void *
base, size_t
nitems, size_t
size, int (*
compar)(const void *, const void *));
The method of comparing is specified by the compar function. This function takes two arguments, the first is the key pointer and the second is the current element in the array being compared. This function must return less than zero if the compared value is less than the specified key. It must return zero if the compared value is equal to the specified key. It must return greater than zero if the compared value is greater than the specified key.
The array must be arranged so that elements that compare less than key are first, elements that equal key are next, and elements that are greater than key are last.
If a match is found, a pointer to this match is returned. Otherwise a null pointer is returned. If multiple matching keys are found, which key is returned is unspecified.
Declaration:
Sorts an array. The beginning of the array is pointed to by base. The array is nitems long with each element in the array size bytes long.void qsort(void *
base, size_t
nitems, size_t
size, int (*
compar)(const void *, const void*));
The elements are sorted in ascending order according to the compar function. This function takes two arguments. These arguments are two elements being compared. This function must return less than zero if the first argument is less than the second. It must return zero if the first argument is equal to the second. It must return greater than zero if the first argument is greater than the second.
If multiple elements are equal, the order they are sorted in the array is unspecified.
No value is returned.
Example:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(void)
{
char string_array[10][50]={"John",
"Jane",
"Mary",
"Rogery",
"Dave",
"Paul",
"Beavis",
"Astro",
"George",
"Elroy"};
/* Sort the list */
qsort(string_array,10,50,strcmp);
/* Search for the item "Elroy" and print it */
printf("%s",bsearch("Elroy",string_array,10,50,strcmp));
return 0;
}
Declaration:
Returns the absolute value of x. Note that in two's compliment that the most maximum number cannot be represented as a positive number. The result in this case is undefined.int abs(int
x);
The absolute value is returned.
Declaration:
Divides numer (numerator) by denom (denominator). The result is stored in the structurediv_t div(int
numer, int
denom);
div_t
which has two members:
int qout;
int rem;
Where quot is the quotient and rem is the remainder. In the case of inexact division, quot
is rounded down to the nearest integer. The value numer is equal to quot * denom + rem
.
The value of the division is returned in the structure.
Declaration:
Returns the absolute value of x. Not that in two's compliment that the most maximum number cannot be represented as a positive number. The result in this case is undefined.long int labs(long int
x);
The absolute value is returned.
Declaration:
Divides numer (numerator) by denom (denominator). The result is stored in the structureldiv_t ldiv(long int
numer, long int
denom);
ldiv_t
which has two members:
long int qout;
long int rem;
Where quot is the quotient and rem is the remainder. In the case of inexact division, quot
is rounded down to the nearest integer. The value numer is equal to quot * denom + rem
.
The value of the division is returned in the structure.
Declaration:
int rand(void);
Returns a pseudo-random number in the range of 0
to RAND_MAX
.
The random number is returned.
Declaration:
This function seeds the random number generator used by the functionvoid srand(unsigned int
seed);
rand
. Seeding
srand
with the same seed will cause rand
to return the same sequence of pseudo-random
numbers. If srand
is not called, rand
acts as if srand(1)
has been called.
No value is returned.
LC_CTYPE
in the
location settings.
Declaration:
Returns the length of a multibyte character pointed to by the argument str. At most n bytes will be examined.int mblen(const char *
str, size_t
n);
If str is a null pointer, then zero is returned if multibyte characters are not state-dependent (shift state). Otherwise a nonzero value is returned if multibyte character are state-dependent.
If str is not null, then the number of bytes that are contained in the multibyte character pointed to by str are returned. Zero is returned if str points to a null character. A value of -1 is returned if str does not point to a valid multibyte character.
Declaration:
Converts the string of multibyte characters pointed to by the argument str to the array pointed to by pwcs. It stores no more than n values into the array. Conversion stops when it reaches the null character or n values have been stored. The null character is stored in the array as zero but is not counted in the return value.size_t mbstowcs(schar_t *
pwcs, const char *
str, size_t
n);
If an invalid multibyte character is reached, then the value -1 is returned. Otherwise the number of values stored in the array is returned not including the terminating zero character.
Declaration:
Examines the multibyte character pointed to by the argument str. The value is converted and stored in the argument pwc if pwc is not null. It scans at most n bytes.int mbtowc(whcar_t *
pwc, const char *
str, size_t
n);
If str is a null pointer, then zero is returned if multibyte characters are not state-dependent (shift state). Otherwise a nonzero value is returned if multibyte character are state-dependent.
If str is not null, then the number of bytes that are contained in the multibyte character pointed to by str are returned. Zero is returned if str points to a null character. A value of -1 is returned if str does not point to a valid multibyte character.
Declaration:
Converts the codes stored in the array pwcs to multibyte characters and stores them in the string str. It copies at most n bytes to the string. If a multibyte character overflows the n constriction, then none of that multibyte character's bytes are copied. Conversion stops when it reaches the null character or n bytes have been written to the string. The null character is stored in the string, but is not counted in the return value.size_t wcstombs(char *
str, const wchar_t *
pwcs, size_t
n);
If an invalid code is reached, the value -1 is returned. Otherwise the number of bytes stored in the string is returned not including the terminating null character.
Declaration:
Examines the code which corresponds to a multibyte character given by the argument wchar. The code is converted to a multibyte character and stored into the string pointed to by the argument str if str is not null.int wctomb(char *
str, wchar_t
wchar);
If str is a null pointer, then zero is returned if multibyte characters are not state-dependent (shift state). Otherwise a nonzero value is returned if multibyte character are state-dependent.
If str is not null, then the number of bytes that are contained in the multibyte character wchar are returned. A value of -1 is returned if wchar is not a valid multibyte character.
Previous Section 2.12 stdio.h |
| Table of Contents | Index | |
Next Section
2.14 string.h |