AVR Libc Home Page |  | AVR Libc Development Pages
Main Page | User Manual | Library Reference | FAQ | Alphabetical Index | Example Projects

# <stdlib.h>: General utilities

## Detailed Description

     #include <stdlib.h>

This file declares some basic C macros and functions as defined by the ISO standard, plus some AVR-specific extensions.

## Data Structures
struct | div_t
struct | ldiv_t
## Non-standard (i.e. non-ISO C) functions.
char * | ltoa (long int __val, char *__s, int __radix)
char * | utoa (unsigned int __val, char *__s, int __radix)
char * | ultoa (unsigned long int __val, char *__s, int __radix)
long | random (void)
void | srandom (unsigned long __seed)
long | random_r (unsigned long *__ctx)
char * | itoa (int __val, char *__s, int __radix)
#define | RANDOM_MAX 0x7FFFFFFF
## Conversion functions for double arguments.
Note that these functions are not located in the default library, `libc.a`, but in the mathematical library, `libm.a`. So when linking the application, the `-lm` option needs to be specified.
char * | dtostre (double __val, char *__s, unsigned char __prec, unsigned char __flags)
char * | dtostrf (double __val, signed char __width, unsigned char __prec, char *__s)
#define | DTOSTR_ALWAYS_SIGN 0x01
#define | DTOSTR_PLUS_SIGN 0x02
#define | DTOSTR_UPPERCASE 0x04
## Defines
#define | RAND_MAX 0x7FFF
## Typedefs
typedef int(* | __compar_fn_t )(const void *, const void *)
## Functions
void | abort (void) __ATTR_NORETURN__
int | abs (int __i)
long | labs (long __i)
void * | bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size, int(*__compar)(const void *, const void *))
div_t | div (int __num, int __denom) __asm__("__divmodhi4")
ldiv_t | ldiv (long __num, long __denom) __asm__("__divmodsi4")
void | qsort (void *__base, size_t __nmemb, size_t __size, __compar_fn_t __compar)
long | strtol (const char *__nptr, char **__endptr, int __base)
unsigned long | strtoul (const char *__nptr, char **__endptr, int __base)
long | atol (const char *__s) __ATTR_PURE__
int | atoi (const char *__s) __ATTR_PURE__
void | exit (int __status) __ATTR_NORETURN__
void * | malloc (size_t __size) __ATTR_MALLOC__
void | free (void *__ptr)
void * | calloc (size_t __nele, size_t __size) __ATTR_MALLOC__
void * | realloc (void *__ptr, size_t __size) __ATTR_MALLOC__
double | strtod (const char *__nptr, char **__endptr)
double | atof (const char *__nptr)
int | rand (void)
void | srand (unsigned int __seed)
int | rand_r (unsigned long *__ctx)
## Variables
size_t | __malloc_margin
char * | __malloc_heap_start
char * | __malloc_heap_end

## Define Documentation

#define DTOSTR_ALWAYS_SIGN 0x01

Bit value that can be passed in `flags` to dtostre().

#define DTOSTR_PLUS_SIGN 0x02

Bit value that can be passed in `flags` to dtostre().

#define DTOSTR_UPPERCASE 0x04

Bit value that can be passed in `flags` to dtostre().

#define RAND_MAX 0x7FFF

Highest number that can be generated by rand().

#define RANDOM_MAX 0x7FFFFFFF

Highest number that can be generated by random().

## Typedef Documentation

typedef int(* __compar_fn_t)(const void *, const void *)

Comparision function type for qsort(), just for convenience.

## Function Documentation

void abort | ( | void |  | ) |

The abort() function causes abnormal program termination to occur. This realization disables interrupts and jumps to _exit() function with argument equal to 1. In the limited AVR environment, execution is effectively halted by entering an infinite loop.

int abs | ( | int | __i | ) |

The abs() function computes the absolute value of the integer `i`.

Note:
  The abs() and labs() functions are builtins of gcc.

double atof | ( | const char * | nptr | ) |

The atof() function converts the initial portion of the string pointed to by nptr to double representation.

It is equivalent to calling

     strtod(nptr, (char **)0);

int atoi | ( | const char * | s | ) |

Convert a string to an integer.

The atoi() function converts the initial portion of the string pointed to by `s` to integer representation. In contrast to

     (int)strtol(s, (char **)NULL, 10);

this function does not detect overflow (`errno` is not changed and the result value is not predictable), uses smaller memory (flash and stack) and works more quickly.

long atol | ( | const char * | s | ) |

Convert a string to a long integer.

The atol() function converts the initial portion of the string pointed to by `s` to long integer representation. In contrast to

     strtol(s, (char **)NULL, 10);

this function does not detect overflow (`errno` is not changed and the result value is not predictable), uses smaller memory (flash and stack) and works more quickly.

void* bsearch | ( | const void * | __key,
|  | const void * | __base,
|  | size_t | __nmemb,
|  | size_t | __size,
|  | int(*)(const void *, const void *) | __compar |
| ) |  |  |

The bsearch() function searches an array of `nmemb` objects, the initial member of which is pointed to by `base`, for a member that matches the object pointed to by `key`. The size of each member of the array is specified by `size`.

The contents of the array should be in ascending sorted order according to the comparison function referenced by `compar`. The `compar` routine is expected to have two arguments which point to the key object and to an array member, in that order, and should return an integer less than, equal to, or greater than zero if the key object is found, respectively, to be less than, to match, or be greater than the array member.

The bsearch() function returns a pointer to a matching member of the array, or a null pointer if no match is found. If two members compare as equal, which member is matched is unspecified.

void* calloc | ( | size_t | __nele,
|  | size_t | __size |
| ) |  |  |

Allocate `nele` elements of `size` each. Identical to calling `malloc()` using `nele * size` as argument, except the allocated memory will be cleared to zero.

div_t div | ( | int | __num,
|  | int | __denom |
| ) |  |  |

The div() function computes the value `num/denom` and returns the quotient and remainder in a structure named `div_t` that contains two int members named `quot` and `rem`.

char* dtostre | ( | double | __val,
|  | char * | __s,
|  | unsigned char | __prec,
|  | unsigned char | __flags |
| ) |  |  |

The dtostre() function converts the double value passed in `val` into an ASCII representation that will be stored under `s`. The caller is responsible for providing sufficient storage in `s`.

Conversion is done in the format `"[-]d.ddde±dd"` where there is one digit before the decimal-point character and the number of digits after it is equal to the precision `prec`; if the precision is zero, no decimal-point character appears. If `flags` has the DTOSTRE_UPPERCASE bit set, the letter `'E'` (rather than `'e'` ) will be used to introduce the exponent. The exponent always contains two digits; if the value is zero, the exponent is `"00"`.

If `flags` has the DTOSTRE_ALWAYS_SIGN bit set, a space character will be placed into the leading position for positive numbers.

If `flags` has the DTOSTRE_PLUS_SIGN bit set, a plus sign will be used instead of a space character in this case.

The dtostre() function returns the pointer to the converted string `s`.

char* dtostrf | ( | double | __val,
|  | signed char | __width,
|  | unsigned char | __prec,
|  | char * | __s |
| ) |  |  |

The dtostrf() function converts the double value passed in `val` into an ASCII representationthat will be stored under `s`. The caller is responsible for providing sufficient storage in `s`.

Conversion is done in the format `"[-]d.ddd"`. The minimum field width of the output string (including the `'`.' and the possible sign for negative values) is given in `width`, and `prec` determines the number of digits after the decimal sign. `width` is signed value, negative for left adjustment.

The dtostrf() function returns the pointer to the converted string `s`.

void exit | ( | int | __status | ) |

The exit() function terminates the application. Since there is no environment to return to, `status` is ignored, and code execution will eventually reach an infinite loop, thereby effectively halting all code processing. Before entering the infinite loop, interrupts are globally disabled.

In a C++ context, global destructors will be called before halting execution.

void free | ( | void * | __ptr | ) |

The free() function causes the allocated memory referenced by `ptr` to be made available for future allocations. If `ptr` is NULL, no action occurs.

char* itoa | ( | int | __val,
|  | char * | __s,
|  | int | __radix |
| ) |  |  |

Convert an integer to a string.

The function itoa() converts the integer value from `val` into an ASCII representation that will be stored under `s`. The caller is responsible for providing sufficient storage in `s`.

Note:
  The minimal size of the buffer `s` depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 * sizeof (int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size.

Warning:
  If the buffer is too small, you risk a buffer overflow.

Conversion is done using the `radix` as base, which may be a number between 2 (binary conversion) and up to 36. If `radix` is greater than 10, the next digit after `'9'` will be the letter `'a'`.

If radix is 10 and val is negative, a minus sign will be prepended.

The itoa() function returns the pointer passed as `s`.

long labs | ( | long | __i | ) |

The labs() function computes the absolute value of the long integer `i`.

Note:
  The abs() and labs() functions are builtins of gcc.

ldiv_t ldiv | ( | long | __num,
|  | long | __denom |
| ) |  |  |

The ldiv() function computes the value `num/denom` and returns the quotient and remainder in a structure named `ldiv_t` that contains two long integer members named `quot` and `rem`.

char* ltoa | ( | long int | __val,
|  | char * | __s,
|  | int | __radix |
| ) |  |  |

Convert a long integer to a string.

The function ltoa() converts the long integer value from `val` into an ASCII representation that will be stored under `s`. The caller is responsible for providing sufficient storage in `s`.

Note:
  The minimal size of the buffer `s` depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 * sizeof (long int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size.

Warning:
  If the buffer is too small, you risk a buffer overflow.

Conversion is done using the `radix` as base, which may be a number between 2 (binary conversion) and up to 36. If `radix` is greater than 10, the next digit after `'9'` will be the letter `'a'`.

If radix is 10 and val is negative, a minus sign will be prepended.

The ltoa() function returns the pointer passed as `s`.

void* malloc | ( | size_t | __size | ) |

The malloc() function allocates `size` bytes of memory. If malloc() fails, a NULL pointer is returned.

Note that malloc() does not initialize the returned memory to zero bytes.

See the chapter about malloc() usage for implementation details.

void qsort | ( | void * | __base,
|  | size_t | __nmemb,
|  | size_t | __size,
|  | __compar_fn_t | __compar |
| ) |  |  |

The qsort() function is a modified partition-exchange sort, or quicksort.

The qsort() function sorts an array of `nmemb` objects, the initial member of which is pointed to by `base`. The size of each object is specified by `size`. The contents of the array base are sorted in ascending order according to a comparison function pointed to by `compar`, which requires two arguments pointing to the objects being compared.

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

int rand | ( | void |  | ) |

The rand() function computes a sequence of pseudo-random integers in the range of 0 to `RAND_MAX` (as defined by the header file <stdlib.h>).

The srand() function sets its argument `seed` as the seed for a new sequence of pseudo-random numbers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.

If no seed value is provided, the functions are automatically seeded with a value of 1.

In compliance with the C standard, these functions operate on `int` arguments. Since the underlying algorithm already uses 32-bit calculations, this causes a loss of precision. See `random()` for an alternate set of functions that retains full 32-bit precision.

int rand_r | ( | unsigned long * | __ctx | ) |

Variant of rand() that stores the context in the user-supplied variable located at `ctx` instead of a static library variable so the function becomes re-entrant.

long random | ( | void |  | ) |

The random() function computes a sequence of pseudo-random integers in the range of 0 to `RANDOM_MAX` (as defined by the header file <stdlib.h>).

The srandom() function sets its argument `seed` as the seed for a new sequence of pseudo-random numbers to be returned by rand(). These sequences are repeatable by calling srandom() with the same seed value.

If no seed value is provided, the functions are automatically seeded with a value of 1.

long random_r | ( | unsigned long * | __ctx | ) |

Variant of random() that stores the context in the user-supplied variable located at `ctx` instead of a static library variable so the function becomes re-entrant.

void* realloc | ( | void * | __ptr,
|  | size_t | __size |
| ) |  |  |

The realloc() function tries to change the size of the region allocated at `ptr` to the new `size` value. It returns a pointer to the new region. The returned pointer might be the same as the old pointer, or a pointer to a completely different region.

The contents of the returned region up to either the old or the new size value (whatever is less) will be identical to the contents of the old region, even in case a new region had to be allocated.

It is acceptable to pass `ptr` as NULL, in which case realloc() will behave identical to malloc().

If the new memory cannot be allocated, realloc() returns NULL, and the region at `ptr` will not be changed.

void srand | ( | unsigned int | __seed | ) |

Pseudo-random number generator seeding; see rand().

void srandom | ( | unsigned long | __seed | ) |

Pseudo-random number generator seeding; see random().

double strtod | ( | const char * | nptr,
|  | char ** | endptr |
| ) |  |  |

The strtod() function converts the initial portion of the string pointed to by nptr to double representation.

The expected form of the string is an optional plus ( `'+'` ) or minus sign ( `'-'` ) followed by a sequence of digits optionally containing a decimal-point character, optionally followed by an exponent. An exponent consists of an `'E'` or `'e'`, followed by an optional plus or minus sign, followed by a sequence of digits.

Leading white-space characters in the string are skipped.

The strtod() function returns the converted value, if any.

If endptr is not `NULL`, a pointer to the character after the last character used in the conversion is stored in the location referenced by endptr.

If no conversion is performed, zero is returned and the value of nptr is stored in the location referenced by endptr.

If the correct value would cause overflow, plus or minus `INFINITY` is returned (according to the sign of the value), and `ERANGE` is stored in `errno`. If the correct value would cause underflow, zero is returned and `ERANGE` is stored in `errno`.

long strtol | ( | const char * | __nptr,
|  | char ** | __endptr,
|  | int | __base |
| ) |  |  |

The strtol() function converts the string in `nptr` to a long value. The conversion is done according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

The string may begin with an arbitrary amount of white space (as determined by isspace()) followed by a single optional `'+'` or `'-'` sign. If `base` is zero or 16, the string may then include a `"0x"` prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is `'0'`, in which case it is taken as 8 (octal).

The remainder of the string is converted to a long value in the obvious manner, stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter `'A'` in either upper or lower case represents 10, `'B'` represents 11, and so forth, with `'Z'` representing 35.)

If `endptr` is not NULL, strtol() stores the address of the first invalid character in `*endptr`. If there were no digits at all, however, strtol() stores the original value of `nptr` in `endptr`. (Thus, if `*nptr` is not `'\0'` but `**endptr` is `'\0'` on return, the entire string was valid.)

The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If no conversion could be performed, 0 is returned. If an overflow or underflow occurs, `errno` is set to ERANGE and the function return value is clamped to `LONG_MIN` or `LONG_MAX`, respectively.

unsigned long strtoul | ( | const char * | __nptr,
|  | char ** | __endptr,
|  | int | __base |
| ) |  |  |

The strtoul() function converts the string in `nptr` to an unsigned long value. The conversion is done according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

The string may begin with an arbitrary amount of white space (as determined by isspace()) followed by a single optional `'+'` or `'-'` sign. If `base` is zero or 16, the string may then include a `"0x"` prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is `'0'`, in which case it is taken as 8 (octal).

The remainder of the string is converted to an unsigned long value in the obvious manner, stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter `'A'` in either upper or lower case represents 10, `'B'` represents 11, and so forth, with `'Z'` representing 35.)

If `endptr` is not NULL, strtoul() stores the address of the first invalid character in `*endptr`. If there were no digits at all, however, strtoul() stores the original value of `nptr` in `endptr`. (Thus, if `*nptr` is not `'\0'` but `**endptr` is `'\0'` on return, the entire string was valid.)

The strtoul() function return either the result of the conversion or, if there was a leading minus sign, the negation of the result of the conversion, unless the original (non-negated) value would overflow; in the latter case, strtoul() returns ULONG_MAX, and `errno` is set to ERANGE. If no conversion could be performed, 0 is returned.

char* ultoa | ( | unsigned long int | __val,
|  | char * | __s,
|  | int | __radix |
| ) |  |  |

Convert an unsigned long integer to a string.

The function ultoa() converts the unsigned long integer value from `val` into an ASCII representation that will be stored under `s`. The caller is responsible for providing sufficient storage in `s`.

Note:
  The minimal size of the buffer `s` depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 * sizeof (unsigned long int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size.

Warning:
  If the buffer is too small, you risk a buffer overflow.

Conversion is done using the `radix` as base, which may be a number between 2 (binary conversion) and up to 36. If `radix` is greater than 10, the next digit after `'9'` will be the letter `'a'`.

The ultoa() function returns the pointer passed as `s`.

char* utoa | ( | unsigned int | __val,
|  | char * | __s,
|  | int | __radix |
| ) |  |  |

Convert an unsigned integer to a string.

The function utoa() converts the unsigned integer value from `val` into an ASCII representation that will be stored under `s`. The caller is responsible for providing sufficient storage in `s`.

Note:
  The minimal size of the buffer `s` depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 * sizeof (unsigned int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size.

Warning:
  If the buffer is too small, you risk a buffer overflow.

Conversion is done using the `radix` as base, which may be a number between 2 (binary conversion) and up to 36. If `radix` is greater than 10, the next digit after `'9'` will be the letter `'a'`.

The utoa() function returns the pointer passed as `s`.

## Variable Documentation

char* __malloc_heap_end

`malloc()` tunable.

char* __malloc_heap_start

`malloc()` tunable.

size_t __malloc_margin

`malloc()` tunable.

Automatically generated by Doxygen 1.5.6 on 4 Dec 2008.