<stdlib.h>¶
Freestanding stdlib subset: conversions, sorting, searching and rand().
#include <stdlib.h>
Functions¶
atoi¶
int atoi(const char *str);
Converts a decimal string to an integer.
Skips leading whitespace, accepts a sign, and stops at the first non-digit. No overflow check.
| Parameter | Direction | Description |
|---|---|---|
str |
in | The null-terminated string. |
Returns: The converted value.
strtol¶
long strtol(const char *nptr, char **endptr, int base);
Converts a string to a long integer.
Accepts leading whitespace, a sign, and a 0x prefix in base 16. No overflow check.
| Parameter | Direction | Description |
|---|---|---|
nptr |
in | The string to convert. |
endptr |
out | Receives a pointer past the last digit used, or nptr if there were none; may be NULL. |
base |
in | 2 to 36, or 0 to pick 16 for "0x", 8 for a leading 0, else 10. |
Returns: The converted value.
strtoul¶
unsigned long strtoul(const char *nptr, char **endptr, int base);
Converts a string to an unsigned long integer.
As strtol(); a minus sign is skipped and ignored.
| Parameter | Direction | Description |
|---|---|---|
nptr |
in | The string to convert. |
endptr |
out | As strtol(); may be NULL. |
base |
in | As strtol(). |
Returns: The converted value.
strtod¶
double strtod(const char *nptr, char **endptr);
Converts a string to a double-precision floating-point number.
Decimal only: digits, an optional fraction and an optional exponent. No inf, nan or hex.
| Parameter | Direction | Description |
|---|---|---|
nptr |
in | The string to convert. |
endptr |
out | Receives a pointer past the last character used; may be NULL. |
Returns: The converted value.
abs¶
int abs(int j);
Computes the absolute value of an integer.
| Parameter | Direction | Description |
|---|---|---|
j |
in | The integer value. |
Returns: The absolute value of the integer.
qsort¶
void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
Uses quicksort to sort an array of elements.
Not stable.
| Parameter | Direction | Description |
|---|---|---|
base |
inout | Pointer to the first element of the array to be sorted. |
nmemb |
in | Number of elements in the array. |
size |
in | Size of each element in bytes. |
compar |
in | Returns less than, equal to or greater than zero as its first argument sorts before, with or after its second. |
bsearch¶
void * bsearch(const void *key, const void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
Performs a binary search on a sorted array.
| Parameter | Direction | Description |
|---|---|---|
key |
in | Pointer to the key to search for. |
base |
in | Pointer to the first element of the sorted array. |
nmemb |
in | Number of elements in the array. |
size |
in | Size of each element in bytes. |
compar |
in | Compares the key (first argument) with an element (second), as for qsort(). |
Returns: Pointer to the matching element in the array, or NULL if the key is not found.
rand¶
int rand(void);
Generates a pseudo-random integer with a linear congruential generator; not for anything security-related.
Returns: A value from 0 to 0x7FFFFFFF.
srand¶
void srand(unsigned int seed);
Seeds the pseudo-random number generator used by rand().
| Parameter | Direction | Description |
|---|---|---|
seed |
in | Seed; 0 is treated as 1. |